Dynamic Memory Allocation in C

Memory Allocation

Dynamic Memory allocations in C with code

Dynamic memory allocation in C is the process of allocating memory during runtime, rather than at compile-time, which is known as static memory allocation. Unlike static memory allocation, dynamic memory allocation enables programs to allocate memory on demand during program execution, rather than reserving a fixed amount of memory at the beginning of the program.

In C, dynamic memory allocation is accomplished with the help of four functions, namely malloc(), calloc(), realloc(), and free(). These functions are part of the standard library (stdlib.h) and are used to allocate and deallocate memory during program runtime. With dynamic memory allocation, a program can obtain the memory it needs at runtime rather than allocating a fixed amount of memory at compile time.

Memory Allocation Functions

Classification on the basis of functions

  • Malloc()
    In C programming, malloc() is a function that is used for dynamic memory allocation. It stands for "memory allocation" and is declared in the stdlib.h header file. The malloc() function is used to dynamically allocate a block of memory of a specified size and returns a pointer to the first byte of the allocated memory. The memory allocated using malloc() remains in the heap until it is explicitly freed using the free() function.
  • calloc()
    It stands for "clear allocation" and is declared in the stdlib.h header file. The calloc() function is comparable to the malloc() function, but with an additional feature of initializing the allocated memory block to zero. It takes two arguments: the number of elements to allocate and the size of each element. It returns a pointer to the first byte of the allocated memory block. The memory allocated using calloc() also remains in the heap until it is explicitly freed using the free() function. This function is commonly used when initializing arrays or structures to avoid unpredictable behavior caused by uninitialized values.
  • realloc()
    In C programming, realloc() is a function used for dynamic memory allocation. It stands for "reallocate" and is declared in the stdlib.h header file. The realloc() function is used to resize the memory block previously allocated with malloc() or calloc(). It takes two arguments: a pointer to the previously allocated memory block and the new size of the block. If there is enough space to expand the block, the realloc() function will adjust the size of the block and return a pointer to the new memory. If there is not enough space, the function may allocate a new block of the requested size, copy the contents of the old block to the new block, and free the old block. The memory block returned by realloc() is guaranteed to be aligned to a multiple of the largest basic alignment of any object type.
  • free()
    In C programming, free() is a function that is used to deallocate memory that was previously allocated using either malloc(), calloc(), or realloc(). The free() function takes a pointer to the memory block that was previously allocated and deallocates it, making it available for future use. It is important to note that once the memory is freed, any data stored in that memory becomes inaccessible and should not be accessed. If a program attempts to access freed memory, it can result in undefined behavior, including crashes and unexpected results. Therefore, it is essential to carefully manage the allocation and deallocation of memory in C programming to avoid such issues.
    Example Code

Allocation Layout


   
In this program, we have first allocated memory using malloc() function for an integer array of size 5. We have then initialized the array with values 1 to 5 and printed them. Finally, we have freed the memory using free() function.