0

我对以下代码有疑问(编译器没有抱怨,但我在运行时收到错误消息 - R6010 abort)。基本上,我创建了一个 Image 类,它从图像中读取数据并将其存储在动态分配的数组中。然后我想将图像数据传递到 int main 中的另一个数组中。由于某种原因,这不起作用。

class Image
{
private:
    char* charImage;
    int TotRows;
    int TotCol;
    int Size;
    int MaxVal;
    char Magic[2];

public:
    Image();
    Image (const Image& Orig);
    ~Image();
    void operator=(const Image&); //overloaded assignment operator
    void ReadImage();
    char ReturnImage(int i);
    int getSize();
};

Image::Image()//constructor
{
    Size = (3 * TotRows * TotCol);
    charImage = new char [Size];
}

Image::Image (const Image& Orig)//copy constructor
{
    TotRows = Orig.TotRows;
    TotCol = Orig.TotCol;
    Size = Orig.Size;
    charImage = new char [Size];
}

Image::~Image()//destructor
{

    delete []charImage;
}

void Image::operator=(const Image& Orig)
{
    TotRows = Orig.TotRows;
    TotCol = Orig.TotCol;
    Size = Orig.Size;
    charImage = new char [Size];

    for (int i = 0; i < Size; i++)
    {
        charImage[i]=Orig.charImage[i];
    }
}

void Image::ReadImage()
{
    //opening original image
    ifstream OldImage;
    OldImage.open ("image2.ppm", ios::in | ios::binary);

    if (!OldImage)
        cout << "\nError: Cannot open image file! " << endl;

    //reading the header of the original image file
    OldImage >> Magic [0] >> Magic [1];

    //if the image is not in the right format, do not proceed!
    if ((Magic [0] != 'P')||(Magic [1] != '6'))
        cout << "\nError: image is in the wrong format!" << endl;

    else
        OldImage >> TotRows >> TotCol >> MaxVal;

    //reading the image in binary format and storing it in the array of characters
    OldImage.read(charImage, Size);

}

char Image::ReturnImage(int i)
{
    return charImage[i];
}
int Image::getSize()
{
    return Size;
}

int main ()
{
    char* charImage;
    int Size;
    Image myImage;
    myImage.ReadImage();
    Size = myImage.getSize();

    charImage= new char [Size];

    for (int i=0; i<Size; i++)
    {
        charImage[i]=myImage.ReturnImage(i);
    }

    delete [] charImage;

    return 0;
}
4

3 回答 3

4

一个明显的错误是您没有在默认构造函数中设置图像的尺寸:

Image::Image()//constructor
{
    Size = (3 * TotRows * TotCol);
    charImage = new char [Size];
}

在这里,TotRowsandTotCol是未初始化的,可以有任何值。

然后,在赋值运算符中,您不会在charImage指向新数组之前释放指向的数组,因此您正在泄漏资源。

于 2013-05-15T11:37:00.480 回答
0

当您创建 Image (myImage) 的实例时,TotRows 和 TotCol 成员未初始化。谁知道构造函数中“Size”的值是多少。

于 2013-05-15T11:36:44.863 回答
0

Apart from others comment,

From my understanding, you do not want to proceed if the image is not in the right format, but still you do reading the image.

  //if the image is not in the right format, do not proceed!
    if ((Magic [0] != 'P')||(Magic [1] != '6'))
        cout << "\nError: image is in the wrong format!" << endl;

    else
        OldImage >> TotRows >> TotCol >> MaxVal;

You read the image irrespective of the image format.

    //reading the image in binary format and storing it in the array of characters
    OldImage.read(charImage, Size);

Hope it helps to some extend.

于 2013-05-15T11:48:57.030 回答