Re: Heap stack Class questons
On Fri, 04 Jun 2010 00:09:14 +0200, "Thomas J. Gritzan"
<phygon_antispam@gmx.de> wrote:
You assume that there are only two kinds of memory. However, global
objects exist neither on the heap nor on the stack. Global variables are
declared in the data section of the executable and are loaded by the PE
loader before the heap exists and before any user code runs.
A related and perhaps more frequently useful concept to understand is
"storage duration". There are three types:
1. Automatic, which applies to non-static local variables as well as
temporaries and function parameters. These are notionally the "stack
variables".
2. Static, which applies to global variables (more generally,
namespace-scope variables), static data members of classes, and local
statics. They live in the data/bss section of the process.
3. Dynamic, which applies to objects created with new. They live on the
heap.
I don't normally care or pause for even a microsecond to think about the
memory addresses where these objects live. Instead, I think about the
lifetime of objects, which is critical to understand, and there are some
subtleties to automatic and static storage duration. OTOH, being able to
recognize memory addresses for stack, heap, and data sections is sometimes
useful when debugging, and I once needed to be able to tell if an object
with static storage duration was inside an EXE or DLL, but that was to
implement something pretty strange.
--
Doug Harrison
Visual C++ MVP