c - 将指针存储为二维数组时遇到问题
问题描述
首先,这是我较大项目中的一段代码,所以请原谅缺少错误检查和其他小细节。为了便于阅读,我排除了这些细节。
该片段应该读取.raw
文件并将数据存储到 auint16_t *
然后将其转换为二维数组,该数组需要使用fwrite()
.
我知道写入的数据image_ptr
是正确的。我相信当我试图将变量保存image_ptr
到函数中命名的二维数组image
中main
或者它发生fwrite()
在我的函数的语句中时,我的问题正在发生save_image
。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define MAX_ROW 2560
#define MAX_COL 2160
uint16_t image[MAX_ROW][MAX_COL];
uint16_t* get_image_data(uint32_t image_index)
{
int result;
char filename[32];
sprintf(filename, "image%d.raw", image_index);
FILE *image_raw = fopen(filename, "rb");
fseek(image_raw, 0, SEEK_END);
long filesize = ftell(image_raw);
rewind(image_raw);
long num_samples = filesize / sizeof(uint16_t);
/*READ IMAGE DATA*/
uint16_t *image_data_ptr = (uint16_t*) malloc(filesize);
size_t num_read = fread(image_data_ptr, sizeof(uint16_t), num_samples, image_raw);
fclose(image_raw);
return image_data_ptr;
}
int save_image()
{
int row, col, pixel_index = 0;
int data_size = MAX_ROW * MAX_COL;
uint16_t * image_data = malloc((sizeof *image_data)*data_size);
image_data = image[0];
FILE *ptr_image_file;
ptr_image_file = fopen("image21.raw", "wb");
for(col = 0; col < MAX_COL; col++)
{
if(**(image + col) != 0)
{
if (fwrite(*(image + col), sizeof(uint16_t), MAX_ROW, ptr_image_file) != MAX_ROW)
{
fprintf(stderr, "Can't write to output file\n");
fclose(ptr_image_file);
return -1;
}
}
}
fclose(ptr_image_file);
return 0;
}
int main()
{
uint16_t * image_ptr = malloc((sizeof *image_ptr)*MAX_ROW*MAX_COL);
//Get Image Data
image_ptr = get_image_data(1);
int row, col, pixel_index = 0;
for(col = 0; col < MAX_COL; col++)
{
for(row = 0; row < MAX_ROW; row++)
{
*(*(image + col) + row) = *(image_ptr + pixel_index);
pixel_index++;
}
}
save_image();
return 0;
}
解决方案
您的代码正在尝试使用全局图像缓冲区和malloc
多个位置的缓冲区,从而导致内存泄漏。
您的转换使用pixel_index
的意图不清楚。
在大多数情况下,您使用指向错误缓冲区的指针,处理没有数据的图像。
您的指针索引不正确。
这是代码的重构版本。
我使用cpp
条件来表示旧代码与新代码:
#if 0
// old code
#else
// new code
#endif
该代码具有错误的注释。我已经编译它但没有测试它:
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define MAX_ROW 2560
#define MAX_COL 2160
#if 0
uint16_t image[MAX_ROW][MAX_COL];
#endif
uint16_t *
get_image_data(uint32_t image_index)
{
#if 0
int result;
#endif
char filename[32];
sprintf(filename, "image%d.raw", image_index);
FILE *image_raw = fopen(filename, "rb");
fseek(image_raw, 0, SEEK_END);
long filesize = ftell(image_raw);
rewind(image_raw);
// NOTE/BUG: what if filesize is odd?
long num_samples = filesize / sizeof(uint16_t);
/* READ IMAGE DATA */
// NOTE/BUG: don't cast the return value of malloc
#if 0
uint16_t *image_data_ptr = (uint16_t *) malloc(filesize);
#else
uint16_t *image_data_ptr = malloc(filesize);
#endif
size_t num_read = fread(image_data_ptr, sizeof(uint16_t), num_samples,
image_raw);
if (num_read != num_samples) {
perror("fread");
exit(1);
}
fclose(image_raw);
return image_data_ptr;
}
int
save_image(uint16_t *image_ptr)
{
#if 0
int row, col, pixel_index = 0;
int data_size = MAX_ROW * MAX_COL;
#else
int row;
#endif
// NOTE/BUG: this blows away the pointer you just got from malloc and created
// a memory leak
// NOTE/BUG: this allocated array has no valid data
#if 0
uint16_t *image_data = malloc((sizeof *image_data) * data_size);
image_data = image[0];
#endif
FILE *ptr_image_file;
ptr_image_file = fopen("image21.raw", "wb");
#if 0
for (col = 0; col < MAX_COL; col++) {
// NOTE/BUG: should always write the data
if (**(image + col) != 0) {
// NOTE/BUG: wrong way to index into image
if (fwrite(*(image + col), sizeof(uint16_t), MAX_ROW, ptr_image_file) != MAX_ROW) {
fprintf(stderr, "Can't write to output file\n");
fclose(ptr_image_file);
return -1;
}
}
}
#else
// output image row-by-row
for (row = 0; row < MAX_ROW; row++) {
// output single row
if (fwrite(&image_ptr[row * MAX_COL], sizeof(uint16_t), MAX_COL,
ptr_image_file) != MAX_COL) {
fprintf(stderr, "Can't write to output file\n");
fclose(ptr_image_file);
return -1;
}
}
#endif
fclose(ptr_image_file);
return 0;
}
int
main(void)
{
// NOTE/BUG: this is blown away with the call to get_image_data
#if 0
uint16_t *image_ptr = malloc((sizeof *image_ptr) * MAX_ROW * MAX_COL);
#else
uint16_t *image_ptr;
#endif
// Get Image Data
image_ptr = get_image_data(1);
int row,
col,
pixel_index = 0;
// NOTE/BUG: this uses the global variable image which has no data
// NOTE/BUG: for loops make access very cache intensive (i.e. slow)
// NOTE/BUG: what transform are you trying to do?
#if 0
for (col = 0; col < MAX_COL; col++) {
for (row = 0; row < MAX_ROW; row++) {
// NOTE/BUG: wrong way to index
*(*(image + col) + row) = *(image_ptr + pixel_index);
pixel_index++;
}
}
#else
for (row = 0; row < MAX_ROW; row++) {
for (col = 0; col < MAX_COL; col++) {
image_ptr[(row * MAX_COL) + col] += pixel_index;
pixel_index++;
}
}
#endif
#if 0
save_image();
#else
save_image(image_ptr);
free(image_ptr);
#endif
return 0;
}
推荐阅读
- azure-devops - VSTS 仪表板的问题
- image-processing - 针织面料的图像处理
- android - 特维利奥。通话在 wifi 关闭或应用程序终止时保持活动状态
- laravel - Laravel 审计 BadMethodCallException 方法审计不存在
- validation - Grails 中的电子邮件验证
- angular - 使用 Angular 5 HttpClient Request 下载 excel 文件
- javascript - JavaScript 事件侦听器不触发拖放
- jquery - 如何获取div内部的频繁值输出?
- javascript - 错误 Wordpress jquery 表单错误说未引用的函数?
- polymer-3.x - 在 stackblitz 中找不到包,即使它存在于 npm 网站中。那怎么安装呢?