Re: How does STL map deal with scoping and memory persistance



Tommo wrote:
> I am storing some key value pairs in a map as follows:
>
> <code>
> bool StaticDataCache::putMarket(uint8_t key,item_t* value)
> {
> typedef pair<uint8_t,item_t*> entry;
> typedef map<uint8_t,item_t*>::iterator iter;
>
> entry newEntry(key,value);
> cout << "About to insert with key:" << key << endl;
> pair<iter,bool> insertSuccess = marketsMap.insert(newEntry);
>
> return true;
> }
> </code>
>
> Now suprisingly to me this actually works when i was expecting it not
> to. As you may notice the pair , newEntry, is local to this function,
> which means, as i understand it, that once this function exits, the
> memory used by newEntry is reclaimed and hence the pair inserted into
> the map would be invalidated. Can anyone tell me why this is working.
> Does the STL pair handle a 'new' behind the scenes??

All STL containers are value based. That means (among other things), then
whenever you add something to an STL container, the item is copied into
memory allocated and managed by the container. Your local copy then has no
link to the copy in the container and can be destroyed immediately.

-cd


.



Relevant Pages

  • Re: unique value for map
    ... tyep which allows same. ... All STL containers (and a lot of non-STL ones) would allow you to insert ... can insert the same element into a linked list and also into a map, ...
    (microsoft.public.vc.stl)
  • Re: CComBSTR, std::endl and stack overflow
    ... be using CComBSTR in a map because STL containers have specific ... you are not allowed to put CComBSTR into any STL container. ... Doing so invokes undefined behavior. ... but not with map. ...
    (microsoft.public.vc.atl)
  • Re: map and vector
    ... > How could I create a map that contains a CString as a key and ... > I have some vector with some data and want to map it to associative ... > What's wrong with my understanding of stl containers? ... you can store the index (unsigned int) of the vector into map. ...
    (microsoft.public.vc.stl)
  • Re: Question about STL containers in multithreaded environment
    ... gioparl wrote: ... or also I have to make a const alias to map also ?? ... All STL containers is allowed to have ... has to make const alias in your case also. ...
    (comp.programming.threads)
  • How does STL map deal with scoping and memory persistance
    ... I am storing some key value pairs in a map as follows: ... entry newEntry; ... As you may notice the pair, newEntry, is local to this function, ... which means, as i understand it, that once this function exits, the ...
    (microsoft.public.dotnet.languages.vc)