2

I've got a map of dynamically allocated object and was wondering what the best way of deleting them was?

I was thinking maybe an interator? Something like:

studentlist::const_iterator deletemem = studentmap.begin();
for(studentlist::const_iterator deletemem=studentmap.begin(); deletemem!=studentmap.end();++deletemem)
{
    Record *z=deletemem->second // Record is the type of object stored in the map student map
    delete z;
 }

But i'm not sure, any help would be much appreciated!

CXR
  • 23
  • 1
  • 3

3 Answers3

4

Your code looks fine. However, manually deleting is probably not exception-safe. You might consider using share_ptr (either the one from Boost, or, if you use C++0x, the standard implementation) for the values or using a boost::ptr_map.

Edit: To delete the actual data in the map, call studentmap.clear() after deleting all the contents. This will remove all elements from the map.

Edit 2: The problem with your solution arises when, for instance due to an exception, your cleanup code does not get called. In that case you leak memory. To overcome this problem, you can employ the RAII-idiom. Two possible ways of doing this are described above.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • Does the code I used above not mean that only the pointer I created gets deleted and not the actual data stored in the map? Thank you for your answer! – CXR May 20 '11 at 12:59
  • Wait, sorry i'm a bit confused, so the code I wrote in does not delete the contents of the map? I am already using studentmap.clear(), just wondering if the elements were dynamically allocated do I need to individually delete them, and if so does the code above work? – CXR May 20 '11 at 13:05
  • If you have a map of dynamically allocated elements, you first have to delete them all the way you did, and then you can clear the map. The links I provided you simply help you do this in a safer and simpler manner. – Björn Pollex May 20 '11 at 13:08
  • @ildjarn: Where do you get these links from? I tried to find them, but couldn't. – Björn Pollex May 20 '11 at 19:06
  • 1
    @Space_C0wb0y : Generally all that's needed is to change the version portion of a URL to `release`; however, for library entry-point links, the URLs from [this page](http://www.boost.org/doc/libs/release/libs/libraries.htm) should be used instead (e.g. [this](http://www.boost.org/doc/libs/release/libs/spirit/index.html) instead of [this](http://www.boost.org/doc/libs/release/libs/spirit/doc/html/index.html) for Boost.Spirit), as these are guaranteed to redirect to the real library entry-point even if it changes later. – ildjarn May 20 '11 at 19:13
1

A better solution would be to encapsulate your dynamically allocated memory inside another object which is stack allocated. This could either be one of your own invention or you could use the shared_ptr class from the new C++ standard or from boost.

Nick
  • 25,026
  • 7
  • 51
  • 83
0

Your code should work, but you should clear the studentmap at the end.

Of course, you have also to invalidate any other object that should hold a copy of the pointers you are deallocating, otherwise your application will probably crash.

Simone
  • 11,655
  • 1
  • 30
  • 43