deleting C++ newed memory

Member
Posts: 321
Joined: 2004.10
Post: #1
Say I've got a pointer (mainPtr) to an array where each element of the array
is itself a pointer to a user defined object called Node.

All Nodes and the array is allocated with the new command.

Will something like

delete [] mainPtr;

free up the array and all associated memory pointed to by the elements,
or do i need to explicitly recover the Nodes memory with separate delete
commands?

Something tells me it is the later, but I'll be happy to be wrong
Smile
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
AFAIK it's the latter. Smile
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #3
It is the latter. If you wanted delete[] to delete all of the content objects too, you should make an array of objects instead of an array of pointers to objects, or use std::auto_ptr to keep track of each object.

Standard warning: but don't use std::auto_ptr with STL containers such as std::vector. Ever. Wink

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Deleting Lua chunks mars 0 1,996 Dec 4, 2005 09:31 PM
Last Post: mars
  Deleting The End of A String With BM Nick 0 2,373 Jul 2, 2005 08:49 AM
Last Post: Nick