user8116022
user8116022

Reputation:

Deleting array memory for a pointer member variable

I am reading a textbook and it seems to indicate that when you have a member variable that's a pointer, you have to put code in the destructor in order to delete the memory.
It's a little unclear but it seems that the code has to look like this:

private:  
double *aPtr;

public:  
~NumberArray(){ delete [ ] aPtr;}

Doesn't this end up being 2 delete commands, though, because the destructor is already deleting the first element in that array? Also, do destructors automatically do their default work, even if you have 1 or more lines in your program for the default destructor? Does the program execute your code first or it executes the code the "automatic" part of the destructor?

For some reason I thought that the delete command is just used for dynamically allocated memory. I guess I am wrong about that?

Upvotes: 0

Views: 437

Answers (2)

Michaël Roy
Michaël Roy

Reputation: 6471

You seem a bit confused about what the difference between delete and delete[].

The first form of delete (without the brackets), is used to destroy dynamic memory allocated for a single object. This form should not be used on arrays, since in that case, the compiler will call the destructor only once.

The second form, with square brackets, guarantees that the destructor will be called for each element of a dynamically allocated array, before the memory used for the array is itself released.

In short:

// first form:
SomeClass* object = new SomeClass;
delete object;                     // SomeClass::~SomeClass is called once, which is good.

// second form
SomeClass* array = new SomeClass[N]; // SomeClass::SomeClass() is called N times.
delete[] array;      // SomeClass::~SomeClass is called N times, which is good.
delete array;        // YIKES!! SomeClass::~SomeClass is called only once. 
                     // This is terrible, as N-1 destructors did not get called.

Upvotes: -1

Doesn't this end up being 2 delete commands, though, because the destructor is already deleting the first element in that array?

The destructor is destructing the pointer (which is a no-op). But it doesn't at all touch what the pointer is pointing at. So there isn't any delete operation going on unless you explicitly write one yourself.

Also, do destructors automatically do their default work, even if you have 1 or more lines in your program for the default destructor?

Yes, destructors will always destroy the members and bases of the object (after the destructor body is executed). But again, the member is aPtr. It's not whatever aPtr is pointing at.

Does the program execute your code first or it executes the code the "automatic" part of the destructor?

Any member of the object is still alive during the destructor body. Which makes sense, since they may be required for cleanup related operations. After that, they are indeed destroyed.

Upvotes: 2

Related Questions