Well, data-memory in a C program falls into three categories:
- Global variables.
- Stack.
- Heap.
These are distinct areas of memory, so they are different addresses. Global variables are given their memory address by the compiler when the program is compiled. As the name indicates, this are all variables declared outside of functions, and also static variables inside functions.
The stack is assigned by the OS when the application is started. The stack holds all variables inside functions, arguments to functions and the return address so that the code can get back to the previous function when this one returns. Whenever you call a function, the stack is counted down (in all systems I'm aware of the stack grows towards zero), and any local variables are formed by the space between the curren stack pointer and the return address back to the previous function. When the function returns, the stack is counted back up again, and the return address is fetched and the code jumps back to where it came from. This means that any variables that "lived" on the stack are "gone" whwn you return to the calling code. The size of the stack is usually fixed by the OS when the application is started. The stack is being constantly re-used by different functions, and the more call levels you have (for example recursive functions), the more stack is being used.
The heap is a place where applications can request "free memory", typically using malloc() or the C++ operator new. It is not "destroyed" when the function returns, instead you have to free the memory by calling free() or use the C++ operator delete. The heap is generally not of a fixed size, as long as there is more space available in the system, it will keep growing, so if you have a machine with lots of memory (say 2GB RAM), you can easily allocate some 1.5-1.8GB of memory in your application. The heap is used to store things that are of "unknown quantity/size".
The problem with "freeing" memory that doesn't belong to the heap is caused by the fact that the heap management functions (malloc for example) have their private data structure to track what is used and what is free on the heap, and that data structure will not work for memory from for example the stack - and even if it does work, what happens when you ask malloc for some memory, it decides to give the stack area back to the function, and they you call some function that also uses that area of the stack - that would cause the same area of memory to be used for different stuff at the same time - which like most things doesn't work!
--
Mats