0% found this document useful (0 votes)
3 views4 pages

Dynamic Memory Allocation

The document provides a C code example demonstrating the use of memory allocation functions: malloc, calloc, realloc, and free. It explains the purpose and behavior of each function, including memory allocation, initialization, resizing, and deallocation. Key points emphasize the importance of checking return values and avoiding memory leaks.

Uploaded by

Ritika Lohiya
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)
3 views4 pages

Dynamic Memory Allocation

The document provides a C code example demonstrating the use of memory allocation functions: malloc, calloc, realloc, and free. It explains the purpose and behavior of each function, including memory allocation, initialization, resizing, and deallocation. Key points emphasize the importance of checking return values and avoiding memory leaks.

Uploaded by

Ritika Lohiya
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/ 4

Here’s an example in C that demonstrates the usage of malloc, calloc, realloc, and

free with explanations for each function.


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

int main() {
// 1. malloc: Allocate memory for an array of 5 integers
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Memory allocated using malloc:\n");


for (int i = 0; i < 5; i++) {
arr[i] = i + 1; // Assign values
printf("%d ", arr[i]);
}
printf("\n");

// 2. calloc: Allocate memory for an array of 5 integers (initialized to 0)


int *carr = (int *)calloc(5, sizeof(int));
if (carr == NULL) {
printf("Memory allocation failed\n");
free(arr);
return 1;
}

printf("Memory allocated using calloc (initialized to 0):\n");


for (int i = 0; i < 5; i++) {
printf("%d ", carr[i]); // Default values will be 0
}
printf("\n");

// 3. realloc: Resize the malloc array to hold 10 integers


arr = (int *)realloc(arr, 10 * sizeof(int));
if (arr == NULL) {
printf("Memory reallocation failed\n");
free(carr); // Free other allocated memory
return 1;
}

printf("Memory after realloc (expanded to 10 integers):\n");


for (int i = 5; i < 10; i++) {
arr[i] = i + 1; // Assign new values for the expanded part
}
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// 4. Free the allocated memory


free(arr);
free(carr);
printf("Memory freed successfully.\n");

return 0;
}
Explanation
1. malloc:
o Allocates a specified number of bytes in memory but does not
initialize the memory.
o The malloc(5 * sizeof(int)) call allocates memory for 5 integers (20
bytes if sizeof(int) is 4).
o The returned memory may contain garbage values.

2. calloc:
o Allocates memory for an array of elements and initializes all elements
to 0.
o The calloc(5, sizeof(int)) call allocates memory for 5 integers and
initializes them to zero.
3. realloc:
o Resizes a previously allocated memory block to a new size.

o The realloc(arr, 10 * sizeof(int)) expands the previously allocated


memory for 5 integers to hold 10 integers.
o If more memory is needed, realloc moves the memory block to a new
location, copying the old data.
4. free:
o Frees the memory allocated by malloc, calloc, or realloc.

o It’s important to free memory to avoid memory leaks.

Output
Memory allocated using malloc:
12345
Memory allocated using calloc (initialized to 0):
00000
Memory after realloc (expanded to 10 integers):
1 2 3 4 5 6 7 8 9 10
Memory freed successfully.

Key Points
 Always check the return value of malloc, calloc, and realloc to ensure
memory allocation was successful.
 Use free to release memory when it’s no longer needed.
 Avoid using freed memory (this is called a "dangling pointer" issue).

You might also like