Memory Management
Memory management is crucial for efficient resource utilization. C provides manual memory management through functions like malloc, calloc, realloc, and free. Understanding memory management allows you to allocate and deallocate memory dynamically.
#include <stdio.h>
#include <stdlib.h>
int main() {
int* numbers = (int*)malloc(5 * sizeof(int));
if (numbers == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
for (int i = 0; i < 5; i++) {
numbers[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
free(numbers);
return 0;
}
No comments:
Post a Comment