Dynamic Memory Allocation Programs in C Programming
Dynamic Memory Allocation Programs in C Programming
Introduction to Dynamic Memory Allocation Programs in C Programming
Dynamic memory allocation programs in C programming help you understand how to use functions like malloc, calloc, realloc, and free in real-world scenarios.
Practicing dynamic memory allocation programs in C programming improves your ability to manage memory efficiently and build advanced applications.
Program 1: Allocate Memory Using malloc()
#include <stdlib.h>
int main() {
int n;
int *ptr;
printf(“Enter number of elements: “);
scanf(“%d”, &n);
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL) {
printf(“Memory not allocated”);
return 1;
}
for(int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
printf(“Elements are:\n”);
for(int i = 0; i < n; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Program 2: Allocate Memory Using calloc()
#include <stdlib.h>
int main() {
int n;
int *ptr;
printf(“Enter number of elements: “);
scanf(“%d”, &n);
ptr = (int*) calloc(n, sizeof(int));
if(ptr == NULL) {
printf(“Memory not allocated”);
return 1;
}
printf(“Elements initialized to zero:\n”);
for(int i = 0; i < n; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Program 3: Resize Memory Using realloc()
#include <stdlib.h>
int main() {
int n;
int *ptr;
printf(“Enter initial size: “);
scanf(“%d”, &n);
ptr = (int*) malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
printf(“Enter new size: “);
scanf(“%d”, &n);
ptr = (int*) realloc(ptr, n * sizeof(int));
for(int i = 0; i < n; i++) {
printf(“%d “, ptr[i]);
}
free(ptr);
return 0;
}
Program 4: Sum of Elements Using Dynamic Memory
#include <stdlib.h>
int main() {
int n, sum = 0;
int *ptr;
printf(“Enter number of elements: “);
scanf(“%d”, &n);
ptr = (int*) malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
printf(“Enter element: “);
scanf(“%d”, &ptr[i]);
sum += ptr[i];
}
printf(“Sum = %d”, sum);
free(ptr);
return 0;
}
Program 5: Find Maximum Element Using Dynamic Memory
#include <stdlib.h>
int main() {
int n, i;
int *ptr, max;
printf(“Enter number of elements: “);
scanf(“%d”, &n);
ptr = (int*) malloc(n * sizeof(int));
for(i = 0; i < n; i++) {
scanf(“%d”, &ptr[i]);
}
max = ptr[0];
for(i = 1; i < n; i++) {
if(ptr[i] > max) {
max = ptr[i];
}
}
printf(“Maximum = %d”, max);
free(ptr);
return 0;
}
Program 6: Dynamic Array with User Input
#include <stdlib.h>
int main() {
int n;
int *arr;
printf(“Enter size: “);
scanf(“%d”, &n);
arr = (int*) malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
printf(“Enter value: “);
scanf(“%d”, &arr[i]);
}
printf(“Array elements:\n”);
for(int i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
free(arr);
return 0;
}
Key Concepts Covered
Important Topics
- malloc, calloc, realloc usage
- Dynamic arrays
- Memory resizing
- Real-world problem solving
Advantages of Practicing Dynamic Memory Programs
Key Benefits
- Better memory management
- Strong programming logic
- Useful in data structures
- Real-world application
Start Learning C Programming
Practice dynamic memory allocation programs to master advanced C programming concepts.
Summary
Dynamic memory allocation programs in C programming help you understand how to allocate, use, and free memory efficiently using different functions.
FAQs
What are dynamic memory allocation programs?
Programs that use malloc, calloc, realloc, and free.
Why practice these programs?
To improve memory handling and logic.
Which function is used to resize memory?
realloc()
Why is free() important?
To prevent memory leaks.



