Dynamic Memory in C

Dynamic memory allocation in C allows you to allocate memory at runtime. This is useful when the size of data structures or variables is not known before the program runs. The malloc(), calloc(), realloc(), and free() functions in C are used for dynamic memory management.

Definition

Dynamic memory allocation enables programs to use memory efficiently by allocating memory as needed and freeing it when no longer required. This type of memory allocation is done using pointers and allows for flexible memory management at runtime.

Functions for Dynamic Memory Allocation

Syntax

Here are the basic syntaxes for these functions:

Syntax:

                        
pointer = (data_type*) malloc(size_in_bytes);    // malloc
pointer = (data_type*) calloc(num_elements, size_of_element);  // calloc
pointer = (data_type*) realloc(pointer, new_size_in_bytes);   // realloc
free(pointer);  // free
                        
                    

Example: Using malloc and free

This example demonstrates how to dynamically allocate and deallocate memory using malloc() and free().

Example Code:

                        
#include <stdio.h>
#include <stdlib.h>
int main() 
{
    int *arr, i, n;
    
    // Ask the user to enter the number of elements
    printf("Enter number of elements: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    // Dynamically allocate memory for n elements
    arr = (int*) malloc(n * sizeof(int));

    // Check if malloc was successful
    if (arr == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Input the elements
    printf("Enter the elements:\n");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &arr[i]) != 1) {
            printf("Invalid input.\n");
            free(arr);  // Free the allocated memory before exiting
            return 1;
        }
    }

    // Output the elements
    printf("Entered elements are:\n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Free the dynamically allocated memory
    free(arr);

    return 0;        
}
                        
                    

Output

Enter number of elements: 5 Enter the elements: 7 98 6 5 4 Entered elements are: 7 98 6 5 4

Example: Using calloc and realloc

This example demonstrates how to use calloc() and realloc() to allocate memory for an array of integers.

Example :

                        
#include <stdio.h>
#include <stdlib.h>
int main() 
{
    int *arr, i, n;
    
    // Ask the user to enter the number of elements
    printf("Enter number of elements: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    // Dynamically allocate memory for n elements
    arr = (int*) malloc(n * sizeof(int));

    // Check if malloc was successful
    if (arr == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Input the elements
    printf("Enter the elements: \n");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &arr[i]) != 1) {
            printf("Invalid input.\n");
            free(arr);  // Free the allocated memory before exiting
            return 1;
        }
    }

    // Output the elements
    printf("Entered elements are: \n");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    // Free the dynamically allocated memory
    free(arr);

    return 0;       
}
    
                        
                    

Output

Enter number of elements: 3 Enter the elements: 6 8 9 Entered elements are: 6 8 9

Key Points