Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .
Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.
More about......Difference between stack and heap
http://net-informations.com/faq/net/stack-heap.htm
Helsy
Hi,
I'm trying to find the amount of available heap memory in my mbed LPC1768 device. I'm using malloc in a loop, starting with the memory block of 0x8000 bytes and decreasing it until malloc returns a pointer different from NULL. I guess this should be the available heap memory (23924 bytes in my case)... but then if I free the memory allocated in the first loop and in the second loop start to increase the desired amount of heap memory malloc function is returning non NULL pointers with the amounts of memory where it previously (in the first loop) returned NULL (in my case I can allocate 27932 bytes in the second loop).
I wonder why do I get the different values and why are they so different (4008 bytes in my case)?