Skip to main content
GameDev.net gamedev.net
🔒 Locked

another stl::map question

Started by SelethD Jan 25, 2006 at 9:18 AM 2 replies 400+ views
Original Post
SelethD
SelethD
when creating the map object like so std::map I want my key_type to be an int, but I want my data_type to be a pointer to an object, such as myClass. So can I do this... std::map myMap; and if this is a valid way to do this, will I need to free the data manually before I erase an element of the map? and if the program is exiting, do I loop through all remaining elements of the map and make sure i free each pointer before the program ends? any tips would be great thanks :)
SiCrane
SiCrane
If you new it, you must delete it. However, if the map is going to own the object, why not just store the object by value and not by pointer?
Bregma
Bregma
Quote:
Original post by SelethD
will I need to free the data manually before I erase an element of the map?

Yes
Quote:
and if the program is exiting, do I loop through all remaining elements of the map and make sure i free each pointer before the program ends?


Yes, if you have allocated any resources other than memory in your objects. No if all you're worried about is memory, since that's reclaimed by the system.

You might want to read up on smart pointers. they're designed to solve these problems.

Stephen M. Webb
Professional Free Software Developer
SelethD
SelethD
I cant store by value, because there are like 10 objects, but the map might hold 20 pointers to those 10 objects. It would be way memory intensive to hold all of them by value.

Thanks for the answers, I just didnt know if it was possible to hold pointers in the map.

Thanks again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.