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

Memory Management in C: malloc, calloc, realloc, free

The document explains memory allocation functions in C: malloc(), calloc(), realloc(), and free(). It details their purposes, syntax, and provides example code for each function, highlighting how to allocate, initialize, resize, and free memory. Key points emphasize the importance of checking for NULL returns and managing memory to prevent leaks and dangling pointers.

Uploaded by

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

Memory Management in C: malloc, calloc, realloc, free

The document explains memory allocation functions in C: malloc(), calloc(), realloc(), and free(). It details their purposes, syntax, and provides example code for each function, highlighting how to allocate, initialize, resize, and free memory. Key points emphasize the importance of checking for NULL returns and managing memory to prevent leaks and dangling pointers.

Uploaded by

vanshpatel3907
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

malloc() — Memory Allocation

🔹 Purpose:

Allocates a single block of memory of a specified size (in bytes).

Returns a pointer to the first byte of the block.

Does not initialize the memory (contains garbage values).

🔹 Syntax:
ptr = (type *) malloc(size_in_bytes);

🔹 Example:
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
int n = 5;

ptr = (int *) malloc(n * sizeof(int)); // allocate memory for 5 integers

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

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

// Assign values
for (int i = 0; i < n; i++)
ptr[i] = i + 1;

printf("Array elements: ");


for (int i = 0; i < n; i++)
printf("%d ", ptr[i]);

free(ptr); // free memory


return 0;
}

🧩 Output:
Memory allocated using malloc.
Array elements: 1 2 3 4 5

22️⃣2 calloc() — Contiguous Allocation


🔹 Purpose:

Allocates memory for multiple elements of a given size.

Initializes all memory bytes to zero.


Returns a pointer to the allocated block.

🔹 Syntax:
ptr = (type *) calloc(num_of_elements, size_of_each_element);

🔹 Example:
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
int n = 5;

ptr = (int *) calloc(n, sizeof(int)); // allocate memory for 5 integers

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

printf("Memory allocated using calloc.\n");

printf("Array elements (initialized to 0): ");


for (int i = 0; i < n; i++)
printf("%d ", ptr[i]);

free(ptr);
return 0;
}

🧩 Output:
Memory allocated using calloc.
Array elements (initialized to 0): 0 0 0 0 0

3️⃣ realloc() — Re-Allocation


🔹 Purpose:

Used to resize an already allocated memory block.

Can increase or decrease the size.

Preserves the contents up to the minimum of the old and new sizes.

Returns a pointer to the new memory block.

🔹 Syntax:
ptr = (type *) realloc(ptr, new_size_in_bytes);

🔹 Example:
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
int n = 5, new_n = 8;

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

for (int i = 0; i < n; i++)


ptr[i] = i + 1;

printf("Old array: ");


for (int i = 0; i < n; i++)
printf("%d ", ptr[i]);

// Reallocate memory to store more elements


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

for (int i = n; i < new_n; i++)


ptr[i] = i + 1;

printf("\nNew array after realloc: ");


for (int i = 0; i < new_n; i++)
printf("%d ", ptr[i]);

free(ptr);
return 0;
}

🧩 Output:
Old array: 1 2 3 4 5
New array after realloc: 1 2 3 4 5 6 7 8

4️⃣ free() — Freeing Allocated Memory


🔹 Purpose:

Releases (frees) the dynamically allocated memory block.

Helps prevent memory leaks.

After freeing, the pointer becomes invalid (dangling pointer).

🔹 Syntax:
free(ptr);

🔹 Example:
#include <stdio.h>
#include <stdlib.h>

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

if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 0;
}

printf("Memory allocated.\n");
free(ptr); // free the allocated memory
printf("Memory freed.\n");

return 0;
}

🧩 Output:
Memory allocated.
Memory freed.

📋 Comparison Table
FunctionPurposeInitializes Memory?Syntaxmalloc()Allocates a block of memory❌
Nomalloc(size)calloc()Allocates multiple blocks and sets to zero✅ Yescalloc(n,
size)realloc()Changes size of existing blockN/Arealloc(ptr, new_size)free()Frees
allocated memoryN/Afree(ptr)

🧩 Key Points to Remember

Always include <stdlib.h>.

Always check if malloc() / calloc() / realloc() returned NULL.

Always call free() when done using dynamically allocated memory.

After calling free(), set the pointer to NULL to avoid dangling pointer errors.

Would you like me to show one single program that uses all four (malloc, calloc,
realloc, and free) together for better understanding?

You might also like