元画像の幅と高さを倍にして、テンポラリ画像を作成して4倍の画像を作成しようとしています。元の画像上の各ピクセルからの情報を取得し、それを一時画像の4ピクセルに渡します。 このようなものです。一時画像にCのピクセルレベルで画像を大きくしようとすると、0xCDCDCDCDのアクセス違反が発生する
[1] [2]
[3] [4]
:
オリジナル画像
[1] [1] [2] [2 ]
[1] [1] [2] [2]
[3] [3] [4] [4]
[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;
に発生するようです。 この違反の原因とは何ですか?これを解決するには何ができますか?
'for(int i = 0、k = 0; i
デバッガでコードをステップ実行するまでの時間。 –
"行に違反が発生しているようです"は有用な情報ではありません。違反が発生したときに、その行のi、j、k、gの値が役立ちます。それらを印刷してください。 – Elan