Jump to content
  • Advertisement
Sign in to follow this  
rsegal

What's the deal with an STL map of wstring's

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

So I've got a map defined as...
map<wstring, int> myMap
and inserting values into the map as...
m_mapKeyCodes.insert(map<wstring, int>::value_type(L"RightMenu",VK_RMENU));
And I'm wondering internally how all this is playing out. The map has to store wstring's obviously and I'm passing a wchar string to insert. This is implicitly being converted to a wstring. What I'm wondering is, is the wstring which is being created implicitly being inserted into the map or is that implicitly created wstring being copied and that copy is stored in the map. Anyone got any thoughts?

Share this post


Link to post
Share on other sites
Advertisement
After looking at the source code that I have (from MSVC6), it works like I guessed it would. Your code, as you said, implicity creates a wstring object. This wstring object is then passed to the pair<> constructor, which calls a copy constructor on the wstring object. This pair object is passed by reference through a number of functions. Finally, once it is ready to insert the object into the map, it does a single copy of the pair object (which should do just a single copy of the internal wstring object). So in all, it appears that there is one normal constructor from a wchar_t type or whatever, and two copy constructors (from one wstring to another wstring). And finally, once the line is done, the initial wstring object is destructed, and so is the pair object, which also contains a wstring object.

I think that's what goes on. Could be wrong, though; I'm no expert, just somewhat kinda-sorta experienced.

Share this post


Link to post
Share on other sites
Nothing implicitly going on, literal strings are statically allocated and are constant.

So whats basically going on is your constructing a temporary std::pair that in it's constructor call's the copy constructors of both it's members first & second, copys an initializes first & second with the value of the literal string & the constant integer, the instance of pair is passed to insert by reference so no copying occurs there, then with-in insert copy's the pair into the container with in aid of an allocator type.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
Nothing implicitly going on, literal strings are statically allocated and are constant.

So whats basically going on is your constructing a temporary std::pair that in it's constructor call's the copy constructors of both it's members first & second...


There has to be a wstring created implicitly somewhere as that's what the map is storing. So when the temporary pair is created are you saying that's when the wstring to be inserted into the map would be created?

Share this post


Link to post
Share on other sites
Quote:
Original post by rsegal
There has to be a wstring created implicitly somewhere as that's what the map is storing.


yes the pair's member first with be the type wstring it will be initialized with a copy of the literal string, nothing implicit about that, the only implicit thing going on is the literal string thats statically allocated with your program.

Quote:
Original post by rsegal
So when the temporary pair is created are you saying that's when the wstring to be inserted into the map would be created?


no that temporary instance of pair is passed to insert by reference but with-in the member function insert it creates a copy of that pair & then its inserted into the map.

EDIT: you may think there is redundant copying going but there is a good reason for this, if it just naively took the address of the actual value passed to it to insert & that value was was not allocated on the heap then it will eventually be destroyed by scope which would lead to dangling pointer syndrome, and the reason why containers doesn't just take a pointer as an argument is you'll have problems on deciding on ownership & memory managment who is responsible for deleting it?.

So the best way is for STL containers to copy construct there elements on the heap and so they own & manage the memory for it.

If you don't like the default memory model that STL containers use then use a custom allocator, it costs you nothing to use a custom allocator instead of the default allocator type.

[Edited by - snk_kid on September 24, 2004 10:54:30 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
yes the pair's member first with be the type wstring it will be initialized with a copy of the literal string, nothing implicit about that, the only implicit thing going on is the literal string thats statically allocated with your program.


I guess that depends how you define implicit conversion. I pass in a literal string, I never tell it directly to create a wstring to be put in the map. I call that implicit conversion but I believe we are talking about the same thing.

Quote:
Original post by snk_kid
you may think there is redundant copying going but there is a good reason for this, if it just naively took the address of the actual value passed to it to insert & that value was was not allocated on the heap then it will eventually be destroyed by scope which would lead to dangling pointer syndrome, and the reason why containers doesn't just take a pointer as an argument is you'll have problems on deciding on ownership & memory managment who is responsible for deleting it?.


Yeh I guess that's true.

Quote:
Original post by snk_kid
If you don't like the default memory model that STL containers use then use a custom allocator, it costs you nothing to use a custom allocator instead of the default allocator type.


I've thought of doing that but at this point writing custom allocators sort of scares me. Got any recommendations for a place to learn more?

Share this post


Link to post
Share on other sites
Quote:
Original post by rsegal
I've thought of doing that but at this point writing custom allocators sort of scares me. Got any recommendations for a place to learn more?


I don't know of any online resources myself probably lots if you check a search engine out, i know there is a good example of writing an allocator that uses an object pool memory scheme in the book "The C++ programming language - special edition by Bjarne Stroustrup".

Boost has a pooled allocator type you can have a look here but i think its only worth using for small fixed sized objects, i use this when use container that hold pointers/smart pointres.

i know GCC comes with quite a thew custom allocators, one of them that uses C-memory functions malloc etc, this would be good learning resource it's really simple.

Although for a pair of wstring & intenger i'm not sure a pooled memory scheme will be of any use because wstrings are variable sized unless you make them fixed length strings say 15 characters long or something you can even use custom allocators on c++ strings.

I think you should just use the default allocator and until you've profiled your code and actually find & prove an inefficiency/overhead going on there then you can always drop in a custom allocator later.

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!