Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Take charge of memory in C — master static vs dynamic allocation, heap vs stack, malloc/calloc/realloc/free, and avoid leaks with practical examples.
Memory management is a critical aspect of programming in the C language. It involves allocating and deallocating memory for various data structures like arrays, structs, and pointers. Understanding memory management is crucial to prevent memory leaks, segmentation faults, and optimize your code's performance. In this tutorial, we'll cover the basics of memory management in C with examples.
Table of contents [Show]
Memory management in C is the process of allocating and releasing memory resources for program data and variables. Memory in a C program is organized into different regions:
In this tutorial, we will focus on dynamic memory management, which involves allocating and releasing memory from the heap.
C provides three primary functions for dynamic memory allocation:
malloc()malloc(size_t size) allocates a block of memory of the specified size in bytes.NULL.calloc()calloc(size_t num_elements, size_t element_size) allocates memory for an array of num_elements each of size element_size bytes.NULL.realloc()realloc(void* ptr, size_t new_size) changes the size of the previously allocated memory block pointed to by ptr to the new size new_size.NULL.To prevent memory leaks, it is essential to release dynamically allocated memory when it is no longer needed. C provides the free() function for this purpose.
free()free(void* ptr) deallocates the memory block pointed to by ptr.free(), the memory can be reused for other allocations.free() memory to avoid memory leaks.Memory leaks occur when memory is allocated but never deallocated. Tools like Valgrind can help detect memory leaks in C programs. Always make sure to free() dynamically allocated memory to prevent leaks.
Let's go through two examples to illustrate memory management in C.
#include
#include
int main() {
int n;
printf("Enter the number of integers: ");
scanf("%d", &n);
// Allocate memory for an array of integers
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!");
return 1;
}
// Initialize the array
for (int i = 0; i < n; i++) {
arr[i] = i * 10;
}
// Print the array
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Deallocate memory
free(arr);
return 0;
}
In this example, we dynamically allocate memory for an integer array of size n, initialize it, print its elements, and then free the allocated memory.
#include
#include
struct Student {
char name[50];
int age;
};
int main() {
struct Student *s;
// Allocate memory for a struct
s = (struct Student *)malloc(sizeof(struct Student));
if (s == NULL) {
printf("Memory allocation failed!");
return 1;
}
// Initialize the struct
strcpy(s->name, "John");
s->age = 20;
// Print the struct members
printf("Name: %s\n", s->name);
printf("Age: %d\n", s->age);
// Deallocate memory
free(s);
return 0;
}
In this example, we allocate memory for a struct and initialize its members before deallocating the memory.
malloc(), calloc(), or realloc() succeeds by verifying if the returned pointer is not NULL.free() when it is no longer needed to prevent memory leaks.Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


