Need more memory? Dynamic memory to the rescue!

In C, a program can obtain memory at runtime through a process called dynamic memory allocation. This is crucial when you need to allocate memory for data whose size changes while the application is running or when the quantity of memory required is unknown at compile time.

Why Dynamic Memory Allocation?

  1. Flexibility: It allows for the allocation of memory as needed at runtime, which is particularly useful for data structures whose size cannot be determined beforehand, like linked lists or dynamic arrays.

  2. Efficiency: It helps in managing memory usage more efficiently by allocating only the required amount of memory, thus minimizing waste.

  3. Control: It gives the programmer direct control over the memory, allowing for the creation of complex data structures and the management of their lifetimes.

How Dynamic Memory Allocation Works:

In C, dynamic memory allocation is managed through a set of functions in the C standard library (stdlib.h):

  • malloc: Allocates a block of memory of the specified size and returns a pointer to the beginning of the block.

  • calloc: Allocates space for an array of elements, initializes them to zero, and then returns a pointer to the memory.

  • realloc: Resizes a previously allocated memory block, keeping as much of the old data as will fit in the new size.

  • free: Deallocates a previously allocated block of memory, making it available again for future allocations.

Sample Code Using Dynamic Memory Allocation Functions:

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

int main() {
    int *arr;
    int n = 5; // Suppose we want an array for 5 integers

    // Allocate memory using malloc
    arr = (int*)malloc(n * sizeof(int));

    if (arr == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    // Initialize the array
    for (int i = 0; i < n; i++) {
        arr[i] = i;
    }

    // Resize the array to hold 10 integers using realloc
    int new_size = 10;
    arr = (int*)realloc(arr, new_size * sizeof(int));

    if (arr == NULL) {
        fprintf(stderr, "Memory reallocation failed\n");
        return 1;
    }

    // Free the allocated memory
    free(arr);

    return 0;
}

Explanation of Functions:

  • malloc(size_t size): Allocates size bytes of uninitialized storage. If allocation succeeds, a pointer to the allocated memory is returned. If it fails, NULL is returned.

  • calloc(size_t num, size_t size): Allocates space for an array of num elements, each of whose size in bytes is size. The space is initialized to zero.

  • realloc(void *ptr, size_t new_size): Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc. If realloc is successful, it returns a pointer to the newly allocated memory, which may be the same as ptr or a new location. If ptr is NULL, realloc behaves like malloc for the specified size.

  • free(void *ptr): Deallocates the memory block pointed to by ptr, which must have been returned by a previous call to malloc, calloc, or realloc. If ptr is NULL, no action occurs.

To confirm that the allocation was successful, it is crucial to constantly examine the return value of these functions. To prevent memory leaks, any block of memory that has been allocated using malloc, calloc, or realloc should also be freed with free when it is no longer required.