Topic: Image
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 14 May 1994 12:32:18 GMT Raw View
>From: beaucham@phy.ulaval.ca (Dominique Beauchamp)
>
>private:
> PIXEL *array;
> INT size;
>
>And the constructor allows an arraw of 1 pixel as the default size:
>
>IMAGE::IMAGE()
>{
> size=1;
> array=new PIXEL[size];
> assert...
>}
>
>Then the destructor destroy it like this:
>
>IMAGE::~IMAGE()
>{
> delete [] array; // yes I'm using 3.0...
>}
>
>Here, an acces violation occures (SYS3175 on OS/2)
There is nothing wrong with your code, at least that
you have shown. Its is probable that the code you have
not shown -- because you didnt write it -- is the error.
This error is very common, and is almost always due
to not defining an copy constructor and assignment operator.
If you dont define:
struct IMAGE { ....
IMAGE(IMAGE const& x) { .. } // copy constructor
the compiler generates it by copying each element. It copies
the image POINTER. SO:
IMAGE x; // an image
IMAGE y = x; // copy it
..
// y gets destroyed, your destructor deletes the POINTER
// x gets destroyed, your destructor deletes the POINTER
// .. AGAIN!!
You need:
IMAGE(IMAGE const& x) { .. size=x.size;
array = new PIXEL[size];
memcpy(array, x.array, size); // copy the array
};
or something similar. You have to do this for assignment too.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA