CodeToLive

Dynamic Memory Allocation in C

Dynamic memory allocation allows you to allocate memory at runtime using functions like malloc(), calloc(), realloc(), and free(). This is useful when the size of the data is not known at compile time.

Using malloc()

The malloc() function allocates a block of memory of a specified size and returns a pointer to the beginning of the block.


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

int main() {
    int *arr = (int *)malloc(5 * sizeof(int));  // Allocate memory for 5 integers

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

    for (int i = 0; i < 5; i++) {
        arr[i] = i + 1;
    }

    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    free(arr);  // Free the allocated memory
    return 0;
}
      

Using calloc()

The calloc() function allocates memory and initializes all bits to zero.


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

int main() {
    int *arr = (int *)calloc(5, sizeof(int));  // Allocate and initialize memory for 5 integers

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

    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);  // All elements are initialized to 0
    }

    free(arr);  // Free the allocated memory
    return 0;
}
      

Using realloc()

The realloc() function resizes a previously allocated block of memory.


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

int main() {
    int *arr = (int *)malloc(5 * sizeof(int));  // Allocate memory for 5 integers

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

    for (int i = 0; i < 5; i++) {
        arr[i] = i + 1;
    }

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

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

    for (int i = 5; i < 10; i++) {
        arr[i] = i + 1;
    }

    for (int i = 0; i < 10; i++) {
        printf("%d ", arr[i]);
    }

    free(arr);  // Free the allocated memory
    return 0;
}
      

Using free()

The free() function deallocates memory previously allocated by malloc(), calloc(), or realloc().


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

int main() {
    int *arr = (int *)malloc(5 * sizeof(int));  // Allocate memory for 5 integers

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

    for (int i = 0; i < 5; i++) {
        arr[i] = i + 1;
    }

    free(arr);  // Free the allocated memory

    // Accessing freed memory is undefined behavior
    // printf("%d\n", arr[0]);  // Don't do this!

    return 0;
}
      

Dynamic Memory for Structures

You can also allocate memory dynamically for structures.


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

struct Student {
    char name[50];
    int age;
};

int main() {
    struct Student *student = (struct Student *)malloc(sizeof(struct Student));

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

    strcpy(student->name, "John Doe");
    student->age = 20;

    printf("Name: %s\n", student->name);
    printf("Age: %d\n", student->age);

    free(student);  // Free the allocated memory
    return 0;
}
      

Common Pitfalls

Be cautious of common mistakes when using dynamic memory allocation:

Next: Structures