Thread: How do you delete an STL map<long,class *>

  1. #1
    Registered User
    Join Date
    Sep 2003

    How do you delete an STL map<long,class *>

    New to STL. Still trying to get a handle on the right way to use it.

    I have a map storing pointers to other objects:
    class EwMultiTrace: public map<unsigned long, EwTrace*>
    What is the correct way to make sure all the elements of the map are deleted?
    Will the map call "delete()" for each stored element?
    Do I have to delete each element manually?
    If so, how to do that?
    EwMultiTrace::~EwMultiTrace (void)
    	EwMultiTrace::iterator item;
    	for (item = this->begin(); item != this->end(); item++)
    		EwTrace *trace = item->second;
    		delete trace;
    	clear ();
    ... this does not work. Removing elements from underneath the iterator creates memory faults.
    maps have no "front()" to feed you the first element until empty.

    What's the right way to go?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    First off, your code is sound. Remember that each element is an std::pair - destroying the contents of pair::second has no effect on the pair container itself (nor the iterator). If you are having a problem, it is somewhere else in the code.

    Also, note that a map is not generally used to store dynamic data for which it is responsible for, since it is a 'lookup' container. There are other containers that are much more useful for general purpose storage. Just a sidenote.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    Registered User
    Join Date
    May 2003
    Check out boost::shared_ptr for your needs. I tend to discourage containers of "dumb" pointers, in favor of containers of smart ones.

    Nonetheless, your code is sound; if you get a crash, perhaps it is because:

    a) You have multiple pointers that reference the same object, so it's being deleted many times, or
    b) You are deleteing pointer not allocated by new, or
    c) You are dereferencing a pointer after it is deleted.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Thanks for the replies.
    I'm back into C++ programming after 3 years in the world of Java, so I'm re-orienting myself with the tricks of the language.

    I am using the map as a lookup table... its elements are lists. The map helps choose which list to append a sub-element into.

    Maybe the map could be simplified to hold a key and Arraynumber, linking to an array of lists.... more complicated than needed. This works.

    Smart Pointers? auto-pointers? New things to me and I will research them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL: erase - do I need to delete?
    By ryeguy in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2008, 01:20 PM
  2. Replies: 5
    Last Post: 03-06-2006, 04:02 PM
  3. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM