尝试在 C 中使图像在像素级别上变大时获取访问冲突写入位置 0xCDCDCDCD
Getting access violation writing location 0xCDCDCDCD when trying to make an image bigger on pixel level in C
我正在尝试通过制作临时图像将原始图像的宽度加倍,从而使图像的大小扩大 4 倍,并且 height.Then 从原始图像的每个像素获取信息并将其提供给 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;
当程序中断并显示错误时。
是什么导致了这种违规行为,为什么?可以做些什么来解决这个问题?
魔法值 0xCDCDCDCD
表示您正在访问未初始化的堆内存:
0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
具体来说,您正在从堆上的指针读取该值,然后尝试取消引用它,从而导致非法内存访问。
有关 MSVC 使用的更多魔术值,请参阅 In Visual Studio C++, what are the memory allocation representations?。
在外循环内,每次迭代:
- 您初始化
temp.pixels[i]
以指向正确分配的内存块
- 您试图写入
temp.pixels[i]
指向的内存块
- 您试图写入
temp.pixels[i+1]
指向的内存块
但是由于您没有初始化 temp.pixels[i+1]
以指向正确分配的内存块,因此尝试使用此变量访问内存会导致内存访问冲突(或更一般地说,未定义的行为)。
我正在尝试通过制作临时图像将原始图像的宽度加倍,从而使图像的大小扩大 4 倍,并且 height.Then 从原始图像的每个像素获取信息并将其提供给 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;
当程序中断并显示错误时。 是什么导致了这种违规行为,为什么?可以做些什么来解决这个问题?
魔法值 0xCDCDCDCD
表示您正在访问未初始化的堆内存:
0xCDCDCDCD : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
具体来说,您正在从堆上的指针读取该值,然后尝试取消引用它,从而导致非法内存访问。
有关 MSVC 使用的更多魔术值,请参阅 In Visual Studio C++, what are the memory allocation representations?。
在外循环内,每次迭代:
- 您初始化
temp.pixels[i]
以指向正确分配的内存块 - 您试图写入
temp.pixels[i]
指向的内存块
- 您试图写入
temp.pixels[i+1]
指向的内存块
但是由于您没有初始化 temp.pixels[i+1]
以指向正确分配的内存块,因此尝试使用此变量访问内存会导致内存访问冲突(或更一般地说,未定义的行为)。