如前所述,我有一个Zynq SoC (ZC706 Eval Board),我正在尝试从SD卡读取图像。为此,我使用了FatFs (e.html)。在我的代码中,我从文件中读取4096字节,并将其保存到缓冲区中。之后,我将缓冲区复制到一个未签名的char指针,在每次读取操作后,该指针的大小都会增加。
然后我使用realloc,copyU32ArrayToUnsignedCharArray函数中的for循环“失败”,因为size变量被out数组覆盖。
覆盖copyU32ArrayToUnsignedCharArray函数中的“大小”的代码:
u32 buffer[1024];
unsigned char *img = NULL;
bytesreaded = 0;
for (;;) {
br=0;
fr = f_read(&fil, buffer, sizeof(buffer), &br); /* Read a chunk of source file */
if (fr || br == 0)
break; /* error or eof */
img = realloc(img,br);
copyU32ArrayToUnsignedCharArray(buffer, &img[bytesreaded], br/4); // /4 because u32(32 bit) in to unsigned char(8 bit)
bytesreaded += br; // update readed bytes
}有效的守则:
u32 buffer[1024];
unsigned char *img = NULL;
img = malloc(512*512*3+100);
bytesreaded = 0;
for (;;) {
br=0;
fr = f_read(&fil, buffer, sizeof(buffer), &br); /* Read a chunk of source file */
if (fr || br == 0)
break; /* error or eof */
copyU32ArrayToUnsignedCharArray(buffer, &img[bytesreaded], br/4); // /4 because u32(32 bit) in to unsigned char(8 bit)
bytesreaded += br; // update readed bytes
}copyU32ArrayToUnsignedCharArray函数:
void copyU32ArrayToUnsignedCharArray(u32 *in, unsigned char* out, uint size){
int i,x;
x = 0;
for (i = 0; i < size; i++) {
if(size != 1024)
break;
in[i] = Xil_In32BE(&in[i]);
out[x] = (u32) in[i] >> 24;
out[x + 1] = (u32) in[i] >> 16 & 0x00FF;
out[x + 2] = (u32) in[i] >> 8 & 0x0000FF;
out[x + 3] = (u32) in[i] & 0x000000FF;
x += 4;
}
}我想使用realloc,因为我不知道我看到的图像有多大。
更新:
一些不起作用的代码的进一步信息。我调试了它,指向*img的指针不是null,所以realloc成功了。如果我使用gdb,在copyU32ArrayToUnsignedCharArray函数中会发生以下情况:
由衷地,
阿诺
发布于 2016-06-08 18:11:41
我从不喜欢的暗示中解决了问题。问题是堆的大小很小。通过编辑链接器脚本文件来增加堆。
https://stackoverflow.com/questions/37696545
复制相似问题