Dynamic Memory Allocation

Dynamic memory allocation allows you to allocate memory at runtime, which provides flexibility when dealing with variable-sized data structures (like arrays or linked lists) that may not be known at compile time.

2. Memory Management Functions in C

malloc

  • Purpose: Allocates a specified number of bytes and returns a pointer to the allocated memory.

Syntax:

void* malloc(size_t size);

Example:

// Allocates memory for 10 integers
int* ptr = (int*)malloc(10 * sizeof(int));

int* intPtr = (int*)malloc(sizeof(int)); // Allocating memory for an int
char* charPtr = (char*)malloc(10 * sizeof(char)); // Allocating memory for a char array
  • Return Value: Returns a void* pointer to the allocated memory, that is a generic pointer type that can be converted to any other pointer type without an explicit cast . If the allocation fails, it returns NULL.

  • Initialization: The memory allocated by malloc is not initialized, so it contains garbage values.

calloc

Purpose: calloc (contiguous allocation) allocates memory for an array of elements and initializes all bits to zero.

Syntax:

void* calloc(size_t num, size_t size);

Return Value: Returns a pointer to the allocated memory. If the allocation fails, it returns NULL.

Key Features:

  • Memory is initialized to zero.

  • Suitable for allocating arrays.

realloc

Purpose: realloc (reallocation) changes the size of previously allocated memory block.

Syntax:

void* realloc(void* ptr, size_t size);
  • ptr: Pointer to the previously allocated memory block (can be NULL).

  • size: New size in bytes.

Return Value: Returns a pointer to the reallocated memory. If the reallocation fails, it returns NULL, and the original memory block is left untouched (not freed).

Key Features:

  • If the new size is larger, the new memory is uninitialized.

  • If the new size is smaller, it truncates the memory block.

  • If ptr is NULL, realloc behaves like malloc.

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

int main() {
    // Allocate memory for an array of 5 integers
    int* arr = (int*)calloc(5, sizeof(int));

    // Check if memory allocation was successful
    if (arr == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    // Print initialized values (all should be zero)
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]); // Output: 0 0 0 0 0
    }
    printf("\n");

   // Resize the array to hold 10 integers
    int* newArr = (int*)realloc(arr, 10 * sizeof(int));
    // Initialize new elements
    for (int i = 5; i < 10; i++) {
        newArr[i] = (i + 1) * 10; // 60, 70, 80, 90, 100
    }
    // Free the allocated memory
    free(arr);

    return 0;
}

free:

  • Purpose: deallocates memory that was previously allocated by malloc, calloc, or realloc.

  • Syntax:

  •   void free(void* ptr);
    
free(ptr); // Frees the allocated memory
  • Important Note: After calling free, the pointer still points to the memory location, but it is no longer valid. It is good practice to set the pointer to NULL to avoid dangling pointers.

Memory Management Operators in C++