🔒 Locked
Deleting array in C++ destructor
Advertisement
Original Post
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?
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.
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.
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:
Proper implementation:
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]
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]
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)
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:
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]
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]
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.
The C++ standard (in section 3.7.3.1) states that ::operator new(size_t) will throw an exception if memory cannot be allocated.
you can use the nothrow option if you are looking for the old style ( ie return NULL pointer on fail )
-------
Andrew
PlaneShift - A MMORPG in development.
int* i = new(nothrow) int[100];
-------
Andrew
PlaneShift - A MMORPG in development.
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...
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???
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???
Just a final note on the zeroed pointer stuff. It is fine to call delete on a zeroed pointer.
Also http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.5
learn to love the c++ faq lite
Also http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.5
learn to love the c++ faq lite
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement