Stack and Heap memory

                     When you look at your computer memory there are different types of memory allocations. Stack and heap memory are type of memory that mainly related to programming. Allocation of memory is very important at the time of programming. Particular size of memory will be allocated for storing local variables and global variables. Both stack and heap are stored in RAM.  In C programming that support three types of memory allocations

  • Static allocation
  • Dynamic allocation
  • Automatic allocation

A heap is an area of pre-reserved computer main storage that a program process can use to store data in some variable amount. And the data is still there after the program ends. The data can remove only manually by the programmer. Mainly global variables are stored in the heap memory. The heap memory has dynamic memory allocation. If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap.

The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. The data added or removed is in last-in-first-out (LIFO) manner. Mainly local variables are stored in stack. After the program execution the stored data is automatically deleted and this for temporary storage. The memory block in stack has fixed size. If there is not enough memory on the stack to handle the memory being assigned to it, a stack overflow occurs.

The heap memory is slower than stack. Because the heap is a complicated data structure than the stack. For many architectures, allocating memory on the stack is just changing the stack pointer.

About Jithin Mathew