Stack Memory is a special region of your computer ' s memory, stores temporary variables created by each function (including the main()
function). The stack is a "FILO" (first-in, last-out) data structure, which is managed and optimized by the CPU quite closely . Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, is freed (tha T was to say, they was deleted). Once a stack variable is freed, which region of the memory becomes available for the other stack variables.
The heap is a region of your computer's memory that's not managed automatically for you, and are not as tight Ly managedby the CPU. It's a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc()
or calloc()
, which is built-in C functions. Once you had allocated memory on the heap and you were responsible for using to free()
deallocate that memory Once you don ' t need itany more. If you fail-to-do, the your program would have a memory leakthat is known as a. That's, memory on the heap would still be set aside (andwon ' t being availableto other processes). This can leads to a slower PC, as there isn ' t as much memory for other processes.
by JBuildz17
http://forum.brackeys.com/thread/what-are-the-differences-between-stack-and-heap-memory-allocation-c/
< go >stack and Heap