Memory Allocation in C Programming Language

Memory Allocation:- The program instructions, global and static variables are stored in a region known as permanent storage area and the local variables are stored in another area called stack.
The free memory region known as heap is located between these two regions and is available for dynamic allocation during execution of the program.
The size of the heap keeps changing when program is executed due to creation and death of variables that are local to functions and blocks.
Read Also:
  1. Data Types
  2. Variables And Constants
  3. First C Program
Therefore, it is possible to encounter memory "overflow" during dynamic allocation process.
When memory allocation functions fail to locate enough memory requested, they return a null pointer.
There are two types of memory allocation techniques:
1. Static memory allocation:- Static memory allocation refers to the process of allocating memory at compile time before the associated program is executed.
Merits: Static memory allocation has the advantage of modularizing data within a program design in the situation where these data must be retained through the runtime of the program. A defined value is global in its scope ensuring that the same immutable value is used throughout a run for consistency.
Demerits: If the amount of memory needed is not known at compile time then one has to make a guess.
2. Dynamic memory allocation:- Dynamic memory allocation is a process of allocating memory at runtime.
Merits: Memory is allocated on an as needed basis. This helps in removing the inefficiencies inherent to static memory allocation.
Demerits: Dynamic memory allocation is slower than static memory allocation. This is because dynamic memory allocation happens in the heap area. Dynamic memory needs to be carefully deleted after use. They are created in non-contiguous area of memory segment.
Memory Management Functions:- Various memory management functions used for memory allocation are written below:
malloc(): The malloc( ) function is used to allocate heap storage. Allocates request size of bytes and returns a pointer to the first byte of the allocated space.
Syntax of malloc():

Here, ptr is pointer of cast-type.
calloc(): The calloc( ) function is used to allocate the continuous memory on element by element basis. Allocates space for an array elements, initializes them to zero and then returns a pointer to the memory.
Syntax of calloc():

This statement will allocate contiguous space in memory for an array of n elements.
realloc( ): The realloc() function is used to increase or decrease the size of the block of heap memory. Modifies the size of previously allocated space.
Syntax of realloc():

Here, ptr is reallocated with size of newsize. free(): The free() function is used to free a portion of storage within the heap previously allocated by alloc(), malloc(), calloc() or realloc( ).
Syntax of free():

This statement frees the space allocated in the memory pointed by ptr.
Example: Using realloc() and malloc() function:

Using free() function:

Post a comment

1 Comments