Computer memory is a limited resource and we have to manage it efficiently. The process by which computer programs are assigned with physical or virtual memory space is called memory allocation.
The memory allocation can be done either before or at the time of program implementation. There are two techniques for memory allocation: static memory allocation and dynamic memory allocation.
Static Memory Allocation
In this type of allocation, the compiler allocates a fixed amount of memory during compile time and the operating system internally uses a data structure known as stack to manage the memory.
Exact memory requirements must be known in advance as once memory is allocated it can not be changed.
Dynamic Memory Allocation
In this type of allocation system memory is managed at runtime. Dynamic memory management in C programming language is performed using four functions named
free(). These four functions are defined in the C standard library header file
<stdlib.h>. It uses the heap space of the system memory.
Let’s know about each function in detail:
malloc() function is used to allocate a block of memory in the heap. It allocates the user a specified number of bytes but does not initialize. Once allocated, the program accesses this block of memory via a pointer that
The default pointer returned by
malloc() is of the type
void but can be cast into a pointer of any data type. However, if the space is insufficient for the amount of memory requested by
malloc(), then the allocation fails and a
NULL pointer is returned.
Since the size of
int is 4 bytes, the above statement will allocate 20 bytes (4 * 5) of memory.
calloc() function is used to dynamically allocate the specified number of blocks of memory of the specified type. It is very much similar to
malloc() but the difference is that it initializes each block with a default value
0 and it has two parameters as compared to
malloc() which only has one parameter.
The above statement allocates contiguous space in memory for 5 elements each with the size of the
realloc() function is used to dynamically change the memory which was previously allocated. It means that if the memory that was previously allocated using the
calloc() function is not sufficient, so it can be reallocated using the
free() function is used to dynamically de-allocate the memory. The memory allocated using functions
calloc() must be de-allocated manually by the user using the
free() function when not in use. It helps to reduce the wastage of memory by freeing it.