我的程序中有一个小的内存访问问题,我没有找到错误,也许有人可以帮助我。
我创建了一种新类型来存储 rgb 颜色值。这种类型看起来像:
typedef struct pixel {
unsigned char r;
unsigned char g;
unsigned char b;
} pixel;
在我的主程序中,我使用 calloc 创建了一个 2D 动态数组,以存储红色信息。
pixel **pixelvalue = (pixel **) calloc(imginformation.width, sizeof(pixel));
for (i = 0; i < imginformation.width; i++) {
pixelvalue[i] = (pixel *) calloc(imginformation.height, sizeof(pixel));
}
之后我调用我的函数,它读取颜色值以及谁应该将它们保存到数组中。该函数将数组作为参数。
ReadFile(file, imginformation (Stuff like height and so one), pixelvalue (The calloc array));
在那个函数中,我尝试用
pixelvalue[i][j].r = (unsigned char)fgetc(in);
在这里我得到内存访问错误,我做错了什么?
编辑
嗨,首先对缺少的语言感到抱歉,我昨天有点累了:)。
为了理解,我创建了一个像素数组并且元素指向另一个像素数组?像[Point to another 1D array pixel]
什么?
使用像素**pixelvalue = calloc(imginformation.width, sizeof(pixel *));
,我从像素类型创建imginformation.width
多个指针,每个指针都显示到像素,对吗?
如果我错了,如果您能再解释一下,那就太棒了。我真的很想了解它。
@carl-norum 你是什么意思:
“你不应该强制转换 calloc() 的返回值。这样做可以隐藏带有 #include 的错误,这些错误可能会回来咬你”。
? 我使用分配空间作为函数的参数,而不是返回值。
谢谢你的帮助!
格雷茨