Jump to content
  • Advertisement
Sign in to follow this  
Vincent_M

Using STL Maps

This topic is 2124 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

If I have a map, and insert an element like so:

map::<string, MyCustomClass> myMap;
MyCustomClass test1;
MyCustomClass test2;

myMap["hi"] = test1; // adds an element with a key
myMap["hi"] = test2; // replaces

When test2 replaces test1, does test1 get released from memory?

Share this post


Link to post
Share on other sites
Advertisement

Correct, I should have realized that if I spent just a few seconds thinking about it; "replaces" as OP wrote in his comment is more accurate. But the key thing, I believe, is what happens to test1: nothing.

Edited by Brother Bob

Share this post


Link to post
Share on other sites

Once I replace test2 with test1, has this caused a memory leak? I'm wondering if I should be safe, and use insert(), or just check if the key already in use with find(), erase() if valid, then insert() or construct using "[] =" (bracket assignment).

Share this post


Link to post
Share on other sites

Ok, that's good to hear. I thought "new" was used internally, and wanted to make sure since the data is copied into meaning more data is allocated upon performing that operation.

Share this post


Link to post
Share on other sites
The general idea is that the semantics of the STL are such that things just work. If you don't know all the details of how the STL should be used most effectively, you could end up with slow code, but it shouldn't leak.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!