gpt4 book ai didi

c - 将指针存储为二维数组时出现问题

转载 作者:行者123 更新时间:2023-12-05 05:58:27 25 4
gpt4 key购买 nike

首先,这是我较大项目中的一段代码,因此请原谅缺少错误检查和其他次要细节。为了便于阅读,我排除了这些细节。
这个代码片段应该读取一个.raw 文件并将数据存储到一个uint16_t * 中,然后将它转换成一个需要写回的二维数组使用 fwrite() 的相同文件。
我知道写入image_ptr 的数据是正确的。我相信当我尝试将变量 image_ptr 保存到 main 函数中名为 image 的二维数组中时,我的问题就出现了,或者它正在发生在我的 save_image 函数的 fwrite() 语句中。

#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;
}

关于c - 将指针存储为二维数组时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68476192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com