Skip to main content
gamedev.net
🔒 Locked

Deleting array in C++ destructor

Started by Agemaniac Mar 1, 2004 at 4:28 PM 14 replies 1.4k views
Original Post
Agemaniac
Agemaniac
Hi. This is a simple question, but i really dont known if i can delete an array like this: class A { A(); ~A(); B *arrayb; } //im allocating the array like this: arrayb = new B[20]; I have an arraya in the main program like arraya = new A[20]; if i use delete []arraya; and have defined the destructor from class A like: ~A() { delete []arrayb; } will this work?
rypyr
rypyr
You''ve got it mostly right.

quote:
Original post by Agemaniac
//im allocating the array like this:
arrayb = new B[20];



I''m assuming this is done in A''s constructor?

Regards,
Jeff


[ CodeDread ]
MrBeaner
MrBeaner
do this as well:

if(array)
delete[] array;

if for some reason the program could not create memory, and you call delete on a null pointer, you will get an access violation. Putting this code in insures that it could never access a null pointer, because the "new" keyowrd retuns 0 if it fails for any reason.



------------------------------------------
VOTE Patrick O''Grady
Write in Presidential Candidate
The Candidate who Cares.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Fruny
Fruny
That will not work. You need to also define a copy constructor AND an assignment operator. Otherwise, if an A object is ever duplicated, you end up with two pointers to the same B array, and when one of these objects finally gets destroyed, the other is left with a dangling pointer:


void foo(A obj)
{ // parameter gets copied locally. obj.arrayb aliases a.arrayb.

// Do stuff

} // parameter gets destroyed. obj.arrayb (i.e. a.arrayb) is deleted.


int main()
{
A a;
foo(a);
// OOOPS, a.arrayb now points to unallocated memory

}


Proper implementation:
   
#include <algorithm>

class A
{
B* arrayb;
public:
A()
: arrayb( new B[20] )
{};

A(const A& rhs)
: arrayb( new B[20] )
{
std::copy( rhs.arrayb, rhs.arrayb+20, arrayb );
}

A& operator=(const A& rhs)
{
if( this == &rhs ) return *this; // self-assignment guard


std::copy( rhs.arrayb, rhs.arrayb+20, arrayb );

return rhs;
}

~A()
{
delete[] arrayb;
}
};


As a rule of thumb, if you need one of: copy constructor, assignment operator, destructor, you most probably need all three.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on March 1, 2004 6:16:08 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny
Fruny
quote:
Original post by MrBeaner
if for some reason the program could not create memory, and you call delete on a null pointer, you will get an access violation.


Incorrect, calling delete or delete[] on a null pointer is a no-op and therefore perfectly valid. This additional test is unnecessary. Furthermore, if the program could not allocate memory, a std::bad_alloc exception is raised, new does not return zero unless you are using a non-compliant compiler, or the new (nothrow) type form.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
MrBeaner
MrBeaner
I thought that new always returned a memory address, and if it couldn't create memory, it returns 0, which in a windows system is none accesable memory?

I know that i am wrong, since i know you, and you know A LOT, but that is what i had always thought.

edit:
i checked the MSDN and this is what it says:

quote:

If unsuccessful, by default new returns zero.



Is MSVC++ none compliant? If it is, maybe i should change compilers.


------------------------------------------
VOTE Patrick O'Grady
Write in Presidential Candidate
The Candidate who Cares.

[edited by - MrBeaner on March 1, 2004 6:29:53 PM]
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
SiCrane
SiCrane
The page you linked to is stale (written in the year 2000). For information on the behaviour of new in current versions of MSVC see this page. (Summary: ::operator new() will throw a std::bad_alloc exception unless you do something funny when linking.)

The C++ standard (in section 3.7.3.1) states that ::operator new(size_t) will throw an exception if memory cannot be allocated.
acraig
acraig
you can use the nothrow option if you are looking for the old style ( ie return NULL pointer on fail )


int* i = new(nothrow) int[100];


-------
Andrew
PlaneShift - A MMORPG in development.
MrBeaner
MrBeaner
I am looking here, just to get more info, because i never have, and Mein GOTT! (is it with two t''s?) There is a lot of crap!

How do you people read this?

------------------------------------------
VOTE Patrick O''Grady
Write in Presidential Candidate
The Candidate who Cares.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
zealouselixir
zealouselixir
quote:
Original post by MrBeaner
Mein GOTT! (is it with two t''s?)

Yes. However, you could have babelfished it or just not sworn in the first place...

[twitter]warrenm[/twitter]
SiCrane
SiCrane
quote:
Original post by MrBeaner
How do you people read this?


Sugar and/or caffeine.
MrBeaner
MrBeaner
quote:
Original post by SiCrane
quote:
Original post by MrBeaner
How do you people read this?


Sugar and/or caffeine.


LOL...


To ZealousElixir
Whatever.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Agemaniac
Agemaniac
Hi..thanks for the post..but i thought my question was a little confuse.

If i delete arraya like
delete []arraya; //dont worry about the NULL test ..etc.

i know that if arraya have 10 elements all then will be destroyed..but..will the destructor of each one be called?

or..will all the arrayb inside each elemente of arraya be destroyed???
SiCrane
SiCrane
When you call array delete, the destructors for each element in the array should be called. And because the destructors for each A object contains a delete expression for arrayb, each array held by each A object should be deleted as well.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.