473,471 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How does STL map deal with scoping and memory persistance

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

Nov 17 '05 #1
5 911
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
Nov 17 '05 #2
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??

Nov 17 '05 #3
You should delete the objects on your own. In general, always 'delete' what
you 'new'.

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
"Tommo" <ma**********@citigroup.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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??

Nov 17 '05 #4
Tommo wrote:
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
Nov 17 '05 #5
Tommo <ma**********@citigroup.com> 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;
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

--
Sp******@gmx.de 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
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: lawrence | last post by:
I'm not sure how this is normally done, on a large site, perhaps one running Phorum. Occassionally a thread will have hundreds of entries, perhaps a meg or two worth of data. You won't necessarily...
18
by: cs | last post by:
This is the function malloc_m() that should be like malloc() but it should find memory leak, and over bound writing in arrays. How many errors do you see? Thank you...
5
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the...
4
by: No One | last post by:
Is there any mechanism in an ASP.Net application to scope components? I know that forms and the like are set to a request scoping and that is fine, but there are certain components I need to have...
17
by: Barret Bonden | last post by:
As an old programmer just now looking at VB.net I have a question: How does one simply open one form from another ? I don't mean how does one create a new instance of that form , but rather how...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
17
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the...
14
by: Khookie | last post by:
Woah... is it just me or do C programmers don't bother talking about how cool C can be (compared to Lisp, Haskell, etc.) - functionally speaking? // Lexical scoping - via nested functions...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.