Maciej Sobczak wrote:
> wrote:
> > For example, dynamically created objects (using "new") are
> > created on the heap. local objects ( foo() {int x;} ) are on
> > the stack.
> We say that these objects have the "dynamic storage duration"
> and the "automatic storage duration".
In the standard. In practice, heap and stack are usable
synonyms.
Let's not forget, either, that the compiler is not required to
use a contiguous area for any of these. It could very well use
different areas for operator new, operator new[] and malloc, for
example, or allocate stack frames dynamically from the same pool
that malloc uses. (I've actually used a C compiler which did
this.)
> > 1)Where are global non-const objects created?
> In some other place, which is a "static storage". Typically,
> the memory for all objects with static storage duration
> (collectively) is assigned to the process when the program is
> loaded into memory. Alternatively, it may be even part of the
> program (of the file on disk) itself, but any other strategy
> is allowed, as long as the lifetimes of the objects obey the
> rules. It all depends on implementation.
Typically, the compiler will manage two separate "static"
areas, one of which will be write protected when your program
executes (and probably shared amongst multiple instances which
execute simutaneously).
> > 2)Where are global const objects created?
> Same as global non-const. This is static storage. The const
> qualifier affects only what you can do with them, not where
> they are.
With the compilers I use, it depends on the type of the object.
If the object has dynamic initialization or a non-trivial
destructor, it will be allocated with the other static objects.
If it is statically initialized, it will normally be allocated
in the write protected part of the static memory. And if it is
simple enough, and its address is never taken, it might not be
allocated at all.
> > 3)For a function, where are local static objects created?
> > These objects are not initialized until the function is
> > called, but is storage allocated for them even if they are
> > never called?
> Again, static storage.
With the same caveats as above.
> > 4)For a function, where are const type objects created? on
> > the stack as well?
> If they are local, then they have automatic storage, on the
> stack. Again, the const qualifier does not affect where they
> are.
If the type is simple enough, and the address of the object is
never taken, the object probably will not be allocated at all.
Otherwise, if the object is initialized with a constant
expression, and has a trivial constructor and destructor, it
will most likely be allocated in the write protected static
memory -- most likely, because if the address of the object is
taken, and the function is called recursively, this cannot be
done, since the addresses have to differ.
> > 5)For a class, where are static class member objects created?
> Static storage.
With all of the above caveats, of course.
And of course, we're only considering single threaded programs
without dynamically loaded objects here.
In fact, of course, the correct answer for all of the above
questions is really: where ever the compiler wants

.
--
James Kanze GABI Software
http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]