我不明白为什么这会导致我的程序崩溃? [英] I don't understand why this is causeing my program to crash?

查看:25
本文介绍了我不明白为什么这会导致我的程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这会导致我的程序崩溃!?当我编译它时,它会结束程序然后停止响应.

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];

请注意,您必须使用 delete[] 在函数末尾释放临时数组(否则会出现内存泄漏).为了使这个自动化并避免异常安全问题,你应该使用一个智能指针,比如来自 Boost 的 scoped_array 或者(如果你有一个支持新 C++ 标准的编译器)unique_ptr.

Notice that you must deallocate the temporary array at the end of the function with delete[] (otherwise you have a memory leak). 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[]>.

更好的是:你可以只使用 std::vector

Even better: you could just use a std::vector<Pixel>

std::vector<Pixel> tempPixel(image.infoHeader.biWidth * image.infoHeader.biHeight);

让它处理分配/解除分配.

and let it deal with allocation/deallocation.

抢先式答案更正(由于您的新问题):如果最后你要把 tempPixel 赋值给 image.pixels,那么你一定不能 delete[]> tempPixel,否则 image 将被一个指向释放内存的指针替换.

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.

但是你有更大的问题:当你替换 image.pixels 时,你并没有释放它之前指向的内存.所以你应该释放那个内存,然后然后tempPixel分配给它.

But you have bigger problems: when you replace image.pixels you are not deallocating the memory it was pointing to previously. So you should deallocate that memory and then assign tempPixel to it.

所有这些都假设 image.pixels 是用 new 分配的,并且将用 delete[] 释放(否则你会得到一个分配函数/运算符不匹配).

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).

顺便说一句,如果您的图像只是 Windows DIB (BMP) 的某种包装,就像从标题字段名称中看到的那样,您没有考虑到像素线是 4 字节对齐的这一事实(因此,如果您的图像不是 32bpp,则必须分配更多内存并相应地执行像素复制).

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).

这篇关于我不明白为什么这会导致我的程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆