How does STL map deal with scoping and memory persistance

T

Tommo

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??
 
C

Carl Daniel [VC++ MVP]

Tommo said:
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
 
T

Tommo

Being that I am holding pointers ( that have been new'd elsewhere )
when i want to remove elements from this map, should i be deleting the
value under a key or will the container take are of it for me??
 
N

Nishant Sivakumar

You should delete the objects on your own. In general, always 'delete' what
you 'new'.
 
T

Tom Widmer [VC++ MVP]

Tommo said:
Being that I am holding pointers ( that have been new'd elsewhere )
when i want to remove elements from this map, should i be deleting the
value under a key or will the container take are of it for me??

If you allocated the memory, you need to take care of destroying it
(unless you've explicitly passed on the responsibility for deleting it,
e.g. to a smart pointer). The map only takes care of memory and objects
it allocates. So, you should be deleting the value or, ideally, you
should be using a smart pointer rather than a raw pointer.

Tom
 
H

Hendrik Schober

Tommo said:
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;

On a sidenote: The correct entry type for
a 'std::map<K,V>' is 'std::pair<const K,V>'.
Or use 'std::map<K,V>::value_type'.

You should consider using a smart pointer as
the value type.


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 
Top