Re: std::map element in heap or stack?
On 2009-12-26, Michael Tsang <firstname.lastname@example.org> wrote:
I am wondering the inserted element in std::map is located in heap or
stack? For example
std::map<int, double> a;
a = 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,
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
ISO/IEC 2382 (all parts), Information technology---Vocabulary
which makes that document (all parts of it, evidently) part of the C++
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
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.