Link to home
Create AccountLog in
Avatar of bowemc
bowemc

asked on

Stack & Heap Memory Explanation

Hi,

I'm at a total loss as to what stack and heap memory are and keep getting confused about them. I was wondering if any experts know of any good resources, which they think explain the topic tastefully???


Thanks

Avatar of avinthm
avinthm

>> All static allocation goes into stack, like declaration of arrays.
Wrong. local variable goes into stack.
static allocation goes into data area of the program.
ASKER CERTIFIED SOLUTION
Avatar of DrAske
DrAske
Flag of Jordan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Prashant Sabnekar
Dear bowemc,
dont get confused, memory management is a simple thing.
When we declare variables in our programs, we actually allocate some space with a name which is our variable name (identifier). Now this space can be allocated either statically or dynamically.
By statically I mean allocating memory on a stack (exactly speaking a program stack).
By dynamically I mean allocating memory on heap (heap is not the area of the program).
Now when the program terminates, the memory allocated by the compiler on stack will be cleaned up automatically, but the memory created on heap will not be cleaned up automatically, It has to be cleaned explicitly ( delete ), otherwise a program is said to have a 'memory leak'.
Static Allocation
Eg: int n;
char name[25];
No problem
Dynamic allocation
Eg:
int* n;
n=new int;
char* name;
name=new char[25];
needs this, in distructor
delete n;
delete [] name; //[] because this is an array

I hope you understand, I advise to do some programs and test it by some memory tools (memprof etc) to find leaks, If you want any extended help, I am allways there.

All the best

Prashant Sabnekar
>> static allocation goes into data area of the program.
A bit more on this : the compiler decides where to put the static data. It might put it in a separate block of data memory. It might even put it between the heap and stack. Some put it in the code area (which i don't consider to be very secure).

In any case, it's a fixed size block of memory that can't be used for anything else than the static data it was destined for.
>> Now this space can be allocated either statically or dynamically.
Actually, there's 3 storage types : statically (in static memory), dynamically (on the heap), and automatically (on the stack).

>> By statically I mean allocating memory on a stack (exactly speaking a program stack).
static data is generally NOT put on the stack, as there's no point in that ... the stack follows the call flow of the program, while the static data remains for the duration of the program. Some compilers might put it at the bottom of the stack, but as i said : conceptually, that's not logical.

>> By dynamically I mean allocating memory on heap (heap is not the area of the program).
What do you mean by "not the area of the program" ? The heap for a program is a part of the memory allocated to the program by the OS.

>> Now when the program terminates, the memory allocated by the compiler on stack will be cleaned up automatically, but the memory created on heap will not be cleaned up automatically,
When the program terminates, all memory (including the heap) is returned to the OS. This to ensure that problems in the code (like memory leaks) don't affect the operation of the OS or the other software running on the system.

>> It has to be cleaned explicitly ( delete ), otherwise a program is said to have a 'memory leak'.
If you don't free a block of allocated memory, and there's no reference (pointer) to that block any more, then that's called a memory leak, because that block of memory can't be used by the program any more, and is thus effectively memory that "leaked" away.
As i said however : when the program terminates, all memory leaks are effectively undone !