[prog] C++ object creation
Diggy Bell
diggy at dbsoftdev.com
Tue May 27 22:06:40 EST 2003
I'll see if I can remember all this myself...
When a program is loaded, there are several segments of memory that are
setup for it. Here's a rough map.
| Code | Data | Heap -> ... | ... <- Stack |
Your program is loaded with it's executable image first in memory (Code).
The next segment (Data) contains data that was declared in your program
(globals and static variables within functions or objects). Following the
data segment is the Heap. As your program allocates memory at runtime
(malloc or new), the memory is obtained from the heap. The size of the heap
grows upward toward the high end of memory. The final segment is the Stack.
As your program executes, the stack is used to keep track of function return
addresses and local function variables (non-static.) But unlike the heap,
the stack is used in reverse. In other works, the stack is used from the
high end of memory downward.
One of the most nefarious programming bugs is a 'stack collision' or 'stack
overflow'. This occurs if the stack and/or heap grow uncontrollably and
they meet. Suffice it to say, that ain't good...
Hope this helps!
William D. 'Diggy' Bell
Principal
DB Software Development
http://www.dbsoftdev.com
----- Original Message -----
From: "Sue Stones" <suzo at bigpond.net.au>
To: "Kathryn Hogg" <kjh at flyballdogs.com>; <programming at linuxchix.org>
Sent: Tuesday, May 27, 2003 8:05 PM
Subject: Re: [prog] C++ object creation
> On Wed, 28 May 2003 07:34, Kathryn Hogg wrote:
> ><...>
> > If you have a variable declared in function, it will most likely be
> > allocated on the stack for most modern machines.
> ><...>
> > Allocating objects from the heap (aka via "new"), allows you to control
> > their lifetime. They are not deallocated until you call "delete" on
them.
> > This is good when you need it but can lead to memory leaks if you lose
> > track of pointer.
>
> In part this answers the question that I am going to ask, but it also
remids
> me that I don't have a full answer.
>
> Could some one please explain what the heap is, and also compare it to the
> stack.
>
> There is no doubt that I learnt about them years ago, but I had a breack
of
> nearly 10 years in my learning process, during which time I had no contact
> with computers. Consequently I forgot some things, and the heap is
something
> that I have totally forgoten!
>
> Thanks
>
> sue
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>
More information about the Programming
mailing list