0% found this document useful (0 votes)
2 views

Dynamic Memory Allocation

Dynamic Memory Allocation (DMA) in C allows memory to be allocated at runtime using functions from stdlib.h, such as malloc(), calloc(), realloc(), and free(). It is useful for creating flexible programs when data size is unknown at compile time and helps in efficient memory usage. Important practices include checking for successful allocation and freeing memory to prevent leaks.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Dynamic Memory Allocation

Dynamic Memory Allocation (DMA) in C allows memory to be allocated at runtime using functions from stdlib.h, such as malloc(), calloc(), realloc(), and free(). It is useful for creating flexible programs when data size is unknown at compile time and helps in efficient memory usage. Important practices include checking for successful allocation and freeing memory to prevent leaks.

Uploaded by

yesudossj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Dynamic Memory Allocation in C

What is Dynamic Memory Allocation?


 Dynamic Memory Allocation (DMA) means allocating memory during the program
execution (runtime) instead of during compile-time.
 In C, memory can be allocated, reallocated, and freed using special functions from the
stdlib.h header file.

Why use Dynamic Memory Allocation?


 When the size of data is unknown at compile time.
 To create flexible and efficient programs.
 To save memory by allocating only what is needed.

Functions for Dynamic Memory Allocation:


Function Purpose
malloc() Allocates specified number of bytes but does not initialize the memory.
calloc() Allocates specified number of blocks and initializes all bytes to zero.
realloc() Changes the size of previously allocated memory.
free() Frees (releases) dynamically allocated memory back to the system.

1. malloc() - Memory Allocation


 Syntax:

ptr = (castType *) malloc(size_in_bytes);

 Example:

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

2. calloc() - Contiguous Allocation


 Syntax:

ptr = (castType *) calloc(num_elements, size_of_each_element);

 Example:

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

3. realloc() - Re-Allocation
 Syntax:

ptr = realloc(ptr, new_size_in_bytes);

 Example:

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

4. free() - Memory Freeing


 Syntax:

free(ptr);

 Example:

free(ptr);

Important Points:
 Always check if memory allocation is successful by checking if the pointer is NULL.
 After using malloc/calloc, free the memory using free() to avoid memory leaks.
 malloc leaves the memory with garbage values whereas calloc initializes memory to
zero.
Small Example Program:
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
int n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

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

if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}

printf("Enter elements:\n");
for (i = 0; i < n; ++i) {
scanf("%d", ptr + i);
}

printf("You entered:\n");
for (i = 0; i < n; ++i) {
printf("%d ", *(ptr + i));
}

free(ptr); // Free the allocated memory


return 0;
}

You might also like