std::map element in heap or stack?

S

shuisheng

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
 
V

Victor Bazarov

shuisheng said:
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.

If by "heap" and "stack" you mean the free store and automatic storage,
then the default allocator places map elements in the free store. The
map itself (the 'a' object) is most likely in the automatic storage if
its definition is inside a function (or it's a subobject of another
automatic object). If the definition of 'a' is outside of any function,
it's static (and probably known to you as "data segment"), although if
the code fragment is like you presented, the assignment cannot be
outside of a function, so it's unlikely that 'a' is defined outside.

V
 
J

James Kanze

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.

Normally, you don't want to know; knowing violates the
abstraction of std::map.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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

The C++ standard does not actually define the terms "heap" and "stack". The
only part which the standard uses "stack" is "stack unwinding", which is
related to exception handling. Consult the implementation documentation to
know how std::map works.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAks1ziAACgkQG6NzcAXitM/uawCcDEW+OOnxcPp+e0DpMYDeg8ie
nZEAn3TDMhd+/CP7NKRQtMYMpeSaAwLo
=Nb5w
-----END PGP SIGNATURE-----
 
K

Kaz Kylheku

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

The C++ standard does not actually define the terms "heap" and "stack". The
only part which the standard uses "stack" is "stack unwinding", which is
related to exception handling.

The C++ standard doesn't define every technical term that it uses. however,
it includes a technical glossary via a normative reference to this
document:

ISO/IEC 2382 (all parts), Information technology---Vocabulary

which makes that document (all parts of it, evidently) part of the C++
standard.

If you do not have that document, then you effectively do not have the complete
C++ standard; you don't know whether or not the C++ standard defines the term
"stack".

If the C++ standard doesn't define that term, then its use in the description
of exception handling is a defect.

It would be astonishing if the definition of stack (if it exists) were at odds
with what nearly every computer scientist understands as a minimal definition
of a stack: namely that it is a data structure whose principal operations
provide a LIFO storage and access discipline.

Local variable storage in C++ demonstrates the LIFO property, therefore
it is probably acceptable to to refer to it as a stack, barring an unusual
or missing definition in ISO 2382.

``Heap'' isn't used in the standard, but it's a widely understood term which
refers to exactly the same kind of abstraction as ``free store'' in C++ or
``dynamic storage'' in C. In that context, it doesn't describe any particular
structure (contrast with ``binomial heap'' which is something else). It doesn't
pose any obstruction to meaningful or correct dialog.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top