Concept of Stack & Heap ( Dynamic Memory Allocation Part - 1 )

Dynamic Memory Allocation





Definition :- 

The process of allocating memory during runtime of the program is known as dynamic memory  allocation. 

Although C does not inheritively has this property but there are four " memory management function " that can be used to allocate memory during runtime. 

  1. malloc( ) - Allocates required size of memory bytes and returns the pointer to the first byte of the allocated space.
  2. calloc( )  - Allocates an array of memory bytes and each memory block corresponds to a malloc memory allocation and then initialized them to zero and returns the pointer.  
  3. realloc( ) - Increasing the size of the previously allocated space.
  4. free ( ) - Frees previously allocated memory.

Memory allocation Process in C

C Dynamic Memory Allocation - Using malloc() and calloc ...
A diagrammatic representation of the memory allocation in c.

The Concept Of Permanent Storage Area

A diagrammatic representation of the memory allocation in c. Memory in C can be divided into three main parts. These are the permanent storage area, the stack area and the heap.


The permanent storage area is the permanent memory space allocated for program instructions and for the global and static variables which run throughout the course of the program.
The Concept of Stack

Pointers and dynamic memory - stack vs heap - YouTubeThe second class of storage is the stack. The stack is the place where all the data variables created within a function and calling of all the inbuilt functions occurs. As a function ends, all the data simultaneously gets deleted from the stack storage. Like the nomenclature goes, the working and deletion mechanism of stack corresponds to the stack data structure, which works on LIFO ( Last in First Out ). The diagrammatic representation on the right correctly describes the working mechanism. Generally the stack memory of a program is limited and is very small of about 1 mb.

How stack overflow occurs ? 

So when a program becomes sufficiently large where the variables and iteration memory exceeds out the stack memory, an error is encountered where the stack runs out of memory. That error is popularly known as the Stack Overflow. This error can be encountered in our small programs during back recursion, when an improper base terminating condition is given and the recursion runs calling itself infinitely, it causes the stack overflow. The memory is allocated during the compile time of the program. In oder to overcome the problem of running out of memory the concept of heap arises.

The Concept Of Heap
Note that the name heap has nothing to do with heap data structure. It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. If a programmer does not handle this memory well, memory leak can happen in the program. Heap is a considerably large memory than stack which is allocated abruptly and is not accessible during compile time and only pointers can access through it, by pointing at the memory addresses allocated at the heap memory. 

Difference between Heap and Stack memory


PARAMETERSTACKHEAP
BasicMemory is allocated in a contiguous block.Memory is allocated in any random order.
Allocation and DeallocationAutomatic by compiler instructions.Manual by programmer.
CostLessMore
ImplementationHardEasy
Access timeFasterSlower
Main IssueShortage of memoryMemory fragmentation
Locality of referenceExcellentAdequete

Comments

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me