C Dynamic Memory Allocation

What is Dynamic Memory Allocation in C?

Dynamic Memory Allocation (DMA) in C is the process of allocating and releasing memory during program execution.

Unlike static memory allocation, dynamic memory allocation allows memory to be allocated when required and released when it is no longer needed.

C provides memory management functions such as malloc(), calloc(), realloc(), and free() through the stdlib.h library.

Example

#include <stdlib.h>

int *ptr;

ptr = (int *)malloc(sizeof(int));

Explanation

• malloc() allocates memory dynamically.

• sizeof(int) reserves memory for one integer.

• ptr stores the address of the allocated memory.

• The allocated memory can be used during program execution.

• The memory should be released using free() after use.

Why Dynamic Memory Allocation is Important?

Dynamic Memory Allocation is important because it:

• Allocates memory only when required.

• Reduces memory wastage.

• Allows programs to handle variable amounts of data.

• Supports dynamic data structures such as linked lists, stacks, and queues.

• Improves memory management and program flexibility.

Types of Dynamic Memory Allocation Functions

C provides four standard functions for dynamic memory allocation through the stdlib.h library.

The most commonly used functions are:

• malloc()

• calloc()

• realloc()

• free()

Each function is used for a specific memory management task.

malloc()

The malloc() function allocates a specified number of bytes in memory during program execution.

It returns a pointer to the allocated memory block. The allocated memory contains garbage values until initialized.

Example

int *ptr;

ptr = (int *)malloc(sizeof(int));

Explanation

• malloc() allocates memory dynamically.

• sizeof(int) reserves memory for one integer.

• ptr stores the address of the allocated memory.

• The allocated memory is uninitialized.

• Always free the allocated memory after use.

calloc()

The calloc() function allocates memory for multiple elements of the same data type.

Unlike malloc(), calloc() initializes all allocated memory with zero values.

Example

int *ptr;

ptr = (int *)calloc(5, sizeof(int));

Explanation

• calloc() allocates memory for multiple elements.

• 5 specifies the number of elements.

• sizeof(int) specifies the size of each element.

• All allocated memory is initialized to zero.

• calloc() returns the starting address of the allocated memory.

realloc()

The realloc() function changes the size of an already allocated memory block.

It can increase or decrease the allocated memory while preserving the existing data whenever possible.

Example

ptr = (int *)realloc(ptr, 10 * sizeof(int));

Explanation

• realloc() changes the size of previously allocated memory.

• ptr is updated with the new memory address.

• The memory size is increased to hold 10 integers.

• Existing data is preserved whenever possible.

• realloc() helps resize memory without creating a new pointer.

free()

The free() function releases memory that was allocated dynamically.

It helps prevent memory leaks by returning unused memory back to the operating system.

Example

free(ptr);

Explanation

• free() releases dynamically allocated memory.

• ptr should point to valid allocated memory.

• After calling free(), the memory becomes available for reuse.

• Releasing unused memory improves program efficiency.

Advantages of Dynamic Memory Allocation

• Allocates memory only when needed.

• Reduces memory wastage.

• Supports dynamic data structures.

• Allows efficient memory management.

• Improves program flexibility and scalability.

Example Program

Program

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

int main()
{
int *ptr;

ptr = (int *)malloc(sizeof(int));

if(ptr == NULL)
{
printf(“Memory allocation failed.”);
return 1;
}

*ptr = 100;

printf(“Value = %d\n”, *ptr);

free(ptr);

return 0;
}

Output

Value = 100

Explanation

• malloc() allocates memory for one integer.

• ptr stores the address of the allocated memory.

• The NULL check ensures memory allocation was successful.

• *ptr = 100 stores the value in the allocated memory.

• printf() displays the stored value.

• free(ptr) releases the allocated memory and prevents memory leaks.

Key Points

• Dynamic Memory Allocation is performed during program execution.

• malloc(), calloc(), realloc(), and free() are memory management functions.

• malloc() allocates uninitialized memory.

• calloc() allocates zero-initialized memory.

• realloc() changes the size of allocated memory.

• free() releases dynamically allocated memory.

• Always free allocated memory to avoid memory leaks.