0

I have a problem with the following code (compiler not complaining but I get an error message at runtime - R6010 abort). Basically I have created an Image class which reads data from an image and stores it in a dynamically allocated array. I then want to pass the image data into anther array in int main. For some reason this does not work.

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

3 Answers 3

4

One obvious error is that you don't set the dimensions of the image int he default constructor:

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

Here, TotRows and TotCol are uninitialized, and could have any value.

Then, in the assignment operator, you do not deallocate the array pointed at by charImage before making it point to a new array, so you are leaking resources.

Sign up to request clarification or add additional context in comments.

1 Comment

THanks! that's exactely what the problem was. thanks so much for helping me out
0

When you create an instance of Image (myImage), the TotRows and TotCol members are uninitialized. Who knows what's the value of 'Size' in the constructor.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.