[英]I don't understand why this is causeing my program to crash?
I don't understand why this is causing my program to crash!? 我不明白为什么这会导致我的程序崩溃! when i compile it makes it to then end of the program then stops responding.
当我编译它使其然后程序结束然后停止响应。
void rotate90(Image& image)
{
Pixel * tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
for(int r = 0; r < image.infoHeader.biHeight; r ++)
{
for(int c = 0; c < image.infoHeader.biWidth; c++)
{
int f = c+(r*image.infoHeader.biWidth);
int t = (image.infoHeader.biHeight - r - 1) + (image.infoHeader.biWidth-c-1);
tempPixel[t] = image.pixels[f];
}
}
image.pixels =tempPixel ;
delete[] tempPixel;
}
You have to declare that variable before using it... 您必须在使用前声明该变量...
Pixel * tempPixel = new Pixel[image.infoHeader.biWidth * image.infoHeader.biHeight];
Notice that you must deallocate the temporary array at the end of the function with delete[]
(otherwise you have a memory leak). 注意,必须在函数末尾使用
delete[]
释放临时数组(否则会发生内存泄漏)。 To make this automatic and avoid issues with exception safety, you should be using a smart pointer, like scoped_array<Pixel>
from Boost or (if you have a compiler that supports the new C++ standard) unique_ptr<Pixel[]>
. 为了自动执行此操作并避免出现异常安全问题,您应该使用智能指针,例如Boost的
scoped_array<Pixel>
或(如果您有支持新C ++标准的编译器) unique_ptr<Pixel[]>
。
Even better: you could just use a std::vector<Pixel>
更好的是:您可以只使用
std::vector<Pixel>
std::vector<Pixel> tempPixel(image.infoHeader.biWidth * image.infoHeader.biHeight);
and let it deal with allocation/deallocation. 并使其处理分配/解除分配。
Preemptive answer correction (due to your new question ): if in the end you are going to assign tempPixel
to image.pixels
, then you must not delete[]
tempPixel
, otherwise image
will be replaced with a pointer to deallocated memory. 抢答校正(因您的新的问题 ):如果你到底要分配
tempPixel
到image.pixels
,那么你一定不能delete[]
tempPixel
,否则image
将与指针解除分配内存所取代。
But you have bigger problems: when you replace image.pixels
you are not deallocating the memory it was pointing to previously. 但是您有更大的问题:替换
image.pixels
您没有取消分配先前指向的内存。 So you should deallocate that memory and then assign tempPixel
to it. 因此,您应该释放该内存, 然后为其分配
tempPixel
。
All this assuming that image.pixels
was allocated with new
and is going to be deallocated with delete[]
(otherwise you get a mismatch of allocation functions/operators). 所有这些都假设
image.pixels
被分配了new
,并且将被delete[]
释放(否则,分配函数/运算符将不匹配)。
By the way, if your image is just some kind of wrapper for a Windows DIB (BMP) as it seems from the header fields names you are not taking into account the fact that pixel lines are 4-byte aligned (so, if your image is not 32bpp, you must allocate more memory and perform the pixel copy accordingly). 顺便说一句,如果您的图像只是Windows DIB(BMP)的某种包装,从标题字段名称看来,您就没有考虑像素行是4字节对齐的事实(因此,如果您的图像不是32bpp,则必须分配更多内存并相应地执行像素复制)。
change array declaration 更改数组声明
tempPixel[] = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
to 至
Pixel* tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
and remember to delete it with 并记得用删除它
delete[] tempPixel;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.