gpt4 book ai didi

c - 尝试在 C 中使图像在像素级别上变大时获取访问冲突写入位置 0xCDCDCDCD

转载 作者:行者123 更新时间:2023-12-02 15:14:20 25 4
gpt4 key购买 nike

我正在尝试通过制作临时图像来制作其大小的 4 倍的图像,其宽度和高度是原始图像的两倍。然后从原始图像上的每个像素获取信息并将其提供给临时图像上的 4 个像素.像这样。

原图:

[1] [2]

[3] [4]

到临时图像:

[1][1][2][2]

[1][1][2][2]

[3][3][4][4]

[3][3][4][4]

但是我得到一个写入位置 0xCDCDCDCD 的访问冲突。这是我的功能代码:

void makeBigger(Image* plain)
{
Image temp;

temp.height = plain->height * 2;
temp.width = plain->width * 2;

temp.pixels = (Pixel**)malloc(sizeof(Pixel*) * temp.height);
for (int i = 0, k = 0; i < temp.height; i+2, k++)
{
temp.pixels[i] = (Pixel*)malloc(sizeof(Pixel) * temp.width);
for (int j = 0, g = 0; j < temp.width; j+2, g++)
{
temp.pixels[i][j].r = plain->pixels[k][g].r;
temp.pixels[i][j+1].r = plain->pixels[k][g].r;
temp.pixels[i+1][j].r = plain->pixels[k][g].r;
temp.pixels[i+1][j+1].r = plain->pixels[k][g].r;

temp.pixels[i][j].g = plain->pixels[k][g].g;
temp.pixels[i][j+1].g = plain->pixels[k][g].g;
temp.pixels[i+1][j].g = plain->pixels[k][g].g;
temp.pixels[i+1][j+1].g = plain->pixels[k][g].g;

temp.pixels[i][j].b = plain->pixels[k][g].b;
temp.pixels[i][j+1].b = plain->pixels[k][g].b;
temp.pixels[i+1][j].b = plain->pixels[k][g].b;
temp.pixels[i+1][j+1].b = plain->pixels[k][g].b;
}
}

*plain = temp;

}

关于违规似乎发生就行了

temp.pixels[i+1][j].r = plain->pixels[k][g].r;

因为那是程序中断并显示错误的时候。是什么导致了这种违规行为,为什么?可以做些什么来解决这个问题?

最佳答案

在外循环中,在每次迭代时:

  • 您初始化 temp.pixels[i] 以指向正确分配的内存块
  • 您尝试写入 temp.pixels[i] 指向的内存块
  • 您尝试写入 temp.pixels[i+1] 指向的内存块

但由于您没有初始化 temp.pixels[i+1] 以指向正确分配的内存块,因此尝试使用此变量访问内存会导致内存访问冲突(或更一般地说, 未定义的行为)。

关于c - 尝试在 C 中使图像在像素级别上变大时获取访问冲突写入位置 0xCDCDCDCD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398316/

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