How memory allocation done in C Language? Discuss Dynamic memory allocation.


Q.) How memory allocation done in C Language? Discuss Dynamic memory allocation.

Subject: Data Structures

Memory Allocation in C Language

In C programming, memory allocation refers to the process of acquiring memory space during program execution. It allows you to allocate memory dynamically at runtime, enabling you to request memory blocks of desired size as needed and release them when no longer required. This dynamic allocation is performed using the standard library functions malloc(), realloc(), and free().

Dynamic Memory Allocation

Dynamic memory allocation is a crucial feature of C programming that enables flexible memory management during program execution. It involves requesting memory from the operating system when needed and releasing it when it is no longer in use. This dynamic allocation allows efficient utilization of memory resources.

1. malloc() Function: Allocating Memory

The malloc() function is used to allocate a block of memory with a specified size. It takes a single argument, which is the number of bytes to be allocated. The function returns a void pointer that points to the beginning of the allocated memory block. If insufficient memory is available, malloc() returns NULL.

void *malloc(size_t size);

Example:

int *ptr = (int *)malloc(sizeof(int) * 10); // Allocates memory for 10 integers
if (ptr == NULL) {
    // Handle memory allocation failure
}

// Use the allocated memory
ptr[0] = 10;
ptr[1] = 20;

// Free the allocated memory when done
free(ptr);

2. realloc() Function: Resizing Memory

The realloc() function allows you to resize an existing memory block allocated using malloc(). It takes three arguments: the pointer to the existing memory block, the new size of the block, and the old size of the block. The function returns a void pointer that points to the resized memory block. If insufficient memory is available, it returns NULL.

void *realloc(void *ptr, size_t new_size, size_t old_size);

Example:

int *ptr = (int *)malloc(sizeof(int) * 5); // Allocates memory for 5 integers
if (ptr == NULL) {
    // Handle memory allocation failure
}

// Use the allocated memory
ptr[0] = 10;
ptr[1] = 20;

// Resize the memory block to accommodate 10 integers
ptr = (int *)realloc(ptr, sizeof(int) * 10, sizeof(int) * 5);
if (ptr == NULL) {
    // Handle memory reallocation failure
}

// Use the resized memory block
ptr[5] = 30;
ptr[6] = 40;

// Free the allocated memory when done
free(ptr);

3. free() Function: Releasing Memory

The free() function is used to release the memory block allocated using malloc() or realloc(). It takes a single argument, which is the pointer to the memory block to be freed. Once the memory is freed, it becomes available for subsequent allocations.

void free(void *ptr);

Example:

int *ptr = (int *)malloc(sizeof(int) * 10); // Allocates memory for 10 integers
if (ptr == NULL) {
    // Handle memory allocation failure
}

// Use the allocated memory
ptr[0] = 10;
ptr[1] = 20;

// Free the allocated memory when done
free(ptr);

Advantages of Dynamic Memory Allocation:

  • Efficient utilization of memory resources.
  • Flexibility to allocate memory at runtime.
  • Ability to adapt to changing program requirements.
  • Support for data structures like linked lists and dynamic arrays.

Disadvantages of Dynamic Memory Allocation:

  • Overhead of managing memory allocations.
  • Potential for memory leaks if allocated memory is not properly freed.
  • Increased complexity in code due to additional memory management tasks.

It's essential to manage dynamic memory allocation carefully to avoid memory-related issues. Common pitfalls include forgetting to free allocated memory, leading to memory leaks, and improper handling of reallocation operations, causing undefined behavior. Using tools like Valgrind or memory debuggers can help detect and prevent such issues.