From: shuisheng on
Dear All,

I am wondering the inserted element in std::map is located in heap or
stack? For example

std::map<int, double> a;
a[0] = 3.14;

The element of the map: pair<int, double>(0, 3.14) is in heap or
stack? I think to know it is important for memory management.

Thank you a lot ahead,

Shuisheng

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Anders Dalvander on
On Dec 9, 2:49 am, shuisheng <shuishen...(a)yahoo.com> wrote:
> I am wondering the inserted element in std::map is located in heap or
> stack?

Hi Shuisheng,

It all depends on the allocator for each specific map: It could be
allocated on the stack, but most often using the default allocator it
is allocated in the free-store.

Regards,
Anders Dalvander


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
shuisheng wrote:
> I am wondering the inserted element in std::map is located in heap or
> stack? For example
>
> std::map<int, double> a;
> a[0] = 3.14;

Since you can insert elements into the container at runtime, either it
stores them internally (which could store them on the "stack") or
externally (i.e. on the heap).

If it was internally, you would have to resize the object dynamically, but
C++ objects always have a fixed size. Alternatively, you could
allocate "enough" spare memory for internal storage (a fixed-capacity
container), but that isn't really useful.

The only other possibility is to use the heap, which is what is used.

> The element of the map: pair<int, double>(0, 3.14) [...]

Danger: The element type for a map<int, double> is pair<int const, double>!

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Taras Shevchuk on
On 09.12.2009 3:49, shuisheng wrote:
> Dear All,
>
> I am wondering the inserted element in std::map is located in heap or
> stack? For example
>
> std::map<int, double> a;
> a[0] = 3.14;
>
> The element of the map: pair<int, double>(0, 3.14) is in heap or
> stack? I think to know it is important for memory management.
>
> Thank you a lot ahead,
>
> Shuisheng
>
Inserted elements in all std containers are located in heap.

Taras Shevchuk

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran Pusic on
On Dec 9, 2:49 am, shuisheng <shuishen...(a)yahoo.com> wrote:
> Dear All,
>
> I am wondering the inserted element in std::map is located in heap or
> stack? For example
>
> std::map<int, double> a;
> a[0] = 3.14;
>
> The element of the map: pair<int, double>(0, 3.14) is in heap or
> stack? I think to know it is important for memory management.

That's probably implementation-specific, and you can certainly change
the allocator of any STL container. So you would have to consult doc
for standard library you use.

That said, map is usually implemented as a red-black tree and tree
nodes are on heap. (Tree nodes, if I understand correctly, contain
instances of value_type, which are key/value pairs of your map).

Note that stack is a bad storage idea for any container as stack
should be considered a scarce resource. E.g. on 32-bit Linux with
pthreads, you default should be 8MB. It should be 1MB on a 32-bit
Windows XP. That's peanuts compared to a couple of GB of virtual
memory.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]