Topic: NEW and DELETE operators???


Author: beaucham@phy.ulaval.ca (Dominique Beauchamp)
Date: Fri, 13 May 1994 13:59:28 GMT
Raw View
Maybe this is a FAQ but I'm new on this net, so I'm sorry if it is...
I'm currently trying to switch from C to C++ and I want to develope
a class which contains a dynamic array.  In fact, my class is an
image an the array is the pixel map.  The size of this array varies
as the size of the image varies.

The images is defined like this:

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)

An other problem seems to occur when I want to redefine a new size.  If I read an
image from a file, I want to resize the array.

IMAGE::Read(char *name)
{
 ...
 delete [] array; // I assume this erase the old "array" but it doesn't?
 size = newsize; // newsize is computed correctly of course
 array=new PIXEL[size];
 ...
}

Again, I get an acces violation.

Probably my question is trivial but I'm a beginner in C++!  Thank you for the answer,
you can post it on this net or directly to my e-mail address

beaucham@phy.ulaval.ca

Dominique Beauchamp
Quebec City

---
Dominique Beauchamp   | Developer of ViewFITS, a FITS
Labo. d'astrophysique,    | viewer for OS/2 2.1
U. Laval, Ste-Foy, G1K7P4 | Last release: April 1994
beaucham@phy.ulaval.ca    | ftp.cdrom.com