Jump to content
  • Advertisement
Sign in to follow this  
Masanori

std::map iterator and dynamic memory allocation

This topic is 3243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Hope you can help me I'm having a little trouble when I want to delete pointers I allocated in my std::map. Basicly:
#include <map>
#include <string>

int main()
{
    std::map<int, std::string*> map;
    map[0] = new std::string;

    std::map<int, std::string*>::iterator itr;
    itr = map.begin();
    delete *itr;

    return 0;
}


This code doesn't compile when "delete *itr" line comes and I dont understand why since the same works with a std::vector. Thank you for anyhelps.

Share this post


Link to post
Share on other sites
Advertisement
When you get errors, it's always a good idea to include the actual error that you get when you post so that we can understand better what's going on.

In this case, though, I can tell you that *it does not return a reference to the value, it returns an std::pair of the key and the value. So you actually want to say something like:
delete it->second;
Though in reality, you probably don't want to do that, either. You're more than likely much better off using a smart pointer like std::tr1::shared_ptr.

Share this post


Link to post
Share on other sites
Quote:
Original post by Black Knight
Do you have any specific reasons to store pointers to std::string?If not just store string directly.
This is a good point too, but I assumed the use of std::string was just for the purposes of the example...

Share this post


Link to post
Share on other sites
Yeah I thought about smart pointer - *deleted*(something I wrote) - ok I'm a bit tired and I can't recall exactly the reason for why i wanted to do this at first, but I was leaded to the fact that i might want to to that in the futur

Edit : ok, yeah I was almost right about the thing I deleted, I might want to copy the pointer stored in my map to a new map, and I though using pointer would be more efficent than raw copy of my data also the early map item storing my pointer would just be removed (with erase()).

I didn't want to use boost, but I didn't know about a standar smart pointer.

Also, why use itr->second and not first? (I stupidly thought that first pointed to the first map item etc...)

Share this post


Link to post
Share on other sites
The elements of the map are actually "pairs". The first element of such pair, as in your case, being an int and the second a pointer to a std::string.

If you want to delete a dynamically allocated std::string you would want to delete the second element of such a pair.

If you want to remove the pair entirely (you should first delete the std::string tough) you may want to call std::map's method erase.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!