Dynamic Memory Allocation

Dynamic memory allocation is a crucial aspect of programming in C, allowing the allocation and deallocation of memory during program execution. Unlike static memory allocation, where memory is allocated at compile-time and cannot be changed during runtime, dynamic memory allocation enables flexibility by allocating memory dynamically during program execution.

Basics of Dynamic Memory Allocation

In C, dynamic memory allocation is managed through functions from the standard library <stdlib.h>, primarily malloc(), calloc(), realloc(), and free()

  • malloc(): Allocates a block of memory of specified size and returns a pointer to the beginning of the block.
  • calloc(): Allocates a block of memory for an array of elements, initializes them to zero, and returns a pointer to the beginning of the block.
  • realloc(): Resizes the previously allocated memory block, either expanding or shrinking it. It returns a pointer to the resized memory block.
  • free(): Deallocates the memory previously allocated by malloc(), calloc(), or realloc().

Using malloc() Function

Let’s consider an example to understand the usage of malloc()

				
					#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int n = 5;

    ptr = (int *)malloc(n * sizeof(int));

    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    printf("Memory allocated successfully\n");

    // Use the allocated memory

    free(ptr); // Free the allocated memory

    return 0;
}

				
			

Explanation:

  • We declare a pointer ptr to store the address of the allocated memory.
  • n represents the number of elements we want to allocate memory for.
  • malloc() allocates memory for n integers (in bytes) and returns a pointer to the first byte of the allocated memory.
  • We check if memory allocation was successful by comparing the returned pointer with NULL.
  • Finally, we free the allocated memory using the free() function.

Using calloc() Function

Now, let’s see how to use the calloc() function

				
					#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int n = 5;

    ptr = (int *)calloc(n, sizeof(int));

    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    printf("Memory allocated successfully\n");

    // Use the allocated memory

    free(ptr); // Free the allocated memory

    return 0;
}

				
			

Explanation:

  • calloc() allocates memory for n integers (in bytes) and initializes them to zero.
  • Rest of the process is similar to malloc().

Using realloc() Function

The realloc() function is used to resize an already allocated block of memory. Here’s how it works

				
					#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int n = 5;

    ptr = (int *)malloc(n * sizeof(int));

    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    printf("Memory allocated successfully\n");

    // Reallocate memory for 10 integers
    ptr = (int *)realloc(ptr, 10 * sizeof(int));

    if (ptr == NULL) {
        printf("Memory reallocation failed\n");
        return 1;
    }

    printf("Memory reallocated successfully\n");

    // Use the reallocated memory

    free(ptr); // Free the allocated memory

    return 0;
}

				
			

Explanation:

  • We initially allocate memory for n integers using malloc().
  • Then, we reallocate memory for 10 integers using realloc().
  • It returns a pointer to the newly allocated memory. If reallocation fails, it returns NULL.

Dynamic memory allocation in C is a powerful feature that allows programs to manage memory dynamically during runtime, providing flexibility and efficiency. By understanding the functions malloc(), calloc(), realloc(), and free(), programmers can effectively manage memory allocation in their C programs. However, it's important to use dynamic memory allocation judiciously to avoid memory leaks and segmentation faults.Happy coding!❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India