关于如何为双指针正确分配内存,我有些困惑。
我的代码第二次尝试在第一个UCHAR指针数组的第二个索引处存储值时导致段错误。
任何援助将不胜感激。
分配我的双指针:
width = BMP_GetWidth (bmp);
height = BMP_GetHeight (bmp);
depth = BMP_GetDepth (bmp);
r = (UCHAR **) malloc(sizeof(UCHAR *) * height);
g = (UCHAR **) malloc(sizeof(UCHAR *) * height);
b = (UCHAR **) malloc(sizeof(UCHAR *) * height);
init_rgb(bmp, width, height, r, g, b);
尝试利用指针(x = 1失败):
void init_rgb(BMP *bmp, UINT w, UINT h, UCHAR **r, UCHAR **g, UCHAR **b) {
printf("%ld, %ld\n", w, h);
UINT x, y;
for (y = 0; y < h; y++) {
r[y] = (UCHAR *)malloc(sizeof(UCHAR) * w);
for (x = 0; x < w; x++) {
BMP_GetPixelRGB(bmp, x, y, &r[y][x], &g[y][x], &b[y][x]);
printf("FAILING After First Iteration\n");
}
}
}
看起来你需要一条线像
r[y] = (UCHAR *)malloc(sizeof(UCHAR) * w);
为g
和b
也。如果你不这样做,g[y]
并且b[y]
是不确定的。当您引用这些代码时,您的代码可能会失败。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句