Memory Allocation Functions in C Programming
Memory Allocation Functions in C Programming
Introduction to Memory Allocation Functions in C Programming
Memory allocation functions in C programming are used to allocate, manage, and release memory dynamically during program execution.
Understanding memory allocation functions in C programming is essential for building efficient and scalable applications.
What are Memory Allocation Functions in C Programming
Memory allocation functions are predefined functions available in the <stdlib.h> library that help allocate memory from the heap.
List of Memory Allocation Functions
Main Functions
malloc()calloc()realloc()free()
Using malloc() Function in C Programming
Syntax
Example
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(3 * sizeof(int));
if(ptr == NULL) {
printf(“Memory not allocated”);
return 1;
}
for(int i = 0; i < 3; i++) {
ptr[i] = i + 1;
}
for(int i = 0; i < 3; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Using calloc() Function in C Programming
Syntax
Example
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) calloc(4, sizeof(int));
if(ptr == NULL) {
printf(“Memory not allocated”);
return 1;
}
for(int i = 0; i < 4; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Using realloc() Function in C Programming
Syntax
Example
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(2 * sizeof(int));
for(int i = 0; i < 2; i++) {
ptr[i] = i + 1;
}
ptr = (int*) realloc(ptr, 5 * sizeof(int));
for(int i = 2; i < 5; i++) {
ptr[i] = i + 1;
}
for(int i = 0; i < 5; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Using free() Function in C Programming
Syntax
Example
#include <stdlib.h>
int main() {
int *ptr = (int*) malloc(3 * sizeof(int));
if(ptr != NULL) {
free(ptr);
printf(“Memory freed successfully”);
}
return 0;
}
Comparison of Memory Allocation Functions
malloc()
- Allocates memory
- Uninitialized
calloc()
- Allocates memory
- Initializes to zero
realloc()
- Resizes memory
free()
- Releases memory
Example: Combined Use of Memory Functions
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) calloc(3, sizeof(int));
ptr = (int*) realloc(ptr, 6 * sizeof(int));
for(int i = 0; i < 6; i++) {
ptr[i] = i + 10;
}
for(int i = 0; i < 6; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Advantages of Memory Allocation Functions
Key Benefits
- Dynamic memory usage
- Efficient memory management
- Useful in large applications
- Supports data structures
Common Mistakes
Avoid These Errors
- Not checking NULL pointer
- Memory leaks
- Using freed memory
- Incorrect size calculation
Best Practices
Tips
- Always check allocation success
- Free memory after use
- Avoid dangling pointers
- Use sizeof() properly
Start Learning C Programming
Practice memory allocation functions to master dynamic memory in C programming.
Summary
Memory allocation functions in C programming allow dynamic memory management using malloc, calloc, realloc, and free.
FAQs
What are memory allocation functions in C programming?
Functions used to allocate and manage memory.
Which header file is used?
stdlib.h
What is realloc()?
Used to resize memory.
Why use free()?
To release memory.



