![]() |
In C programming, we might allocate memory dynamically for various tasks but what happens when those pieces of memory are no longer needed? If not managed properly, they can lead to memory leaks, wasting valuable resources, and slowing down our program. Therefore, we need to manage memory in C by properly deallocating it when it’s no longer in use to ensure that our programs run efficiently without unnecessary memory consumption. In this article, we will discuss everything in detail about deleting memory in C, why it’s important to free up memory, the common pitfalls we should avoid, and how to deallocate memory correctly with the help of examples. Types of Memory Allocation in CBefore learning about memory deletion, it’s important to understand the basic difference between static and dynamic memory:
Memory Deallocation in CDynamic memory allocation in C allows us to allocate memory at runtime by using the functions like malloc(), calloc(), or realloc() that provide flexibility in memory management. However, it also requires manual deallocation that can be done using the free() function or realloc function when it’s no longer in use to ensure that our programs run efficiently that is used to free or deallocate the dynamically allocated memory and helps in reducing memory wastage and avoiding memory leaks. Syntax to Use free() Function in Cvoid free(void *ptr); Here, ptr is the pointer to the memory block that we want to free or deallocate. ![]() Free() Function in C Examples of Dynamic Memory Deallocation in CThe following examples illustrates how we can delete the memory allocated dynamically using the free() function. Example 1: Deleting Dynamically Allocated Memory Using free()The below C program illustrate how we can use the malloc() function to allocate memory dynamically and free() function to release that memory.
Output ptr freed successfully Example 2: Deleting Dynamically Allocated Memory Using realloc()The below C program illustrates how we can use the calloc() function to allocate memory dynamically and realloc() function to deallocate that memory.
Output Memory deallocated successfully using realloc Example 3: Deleting Dynamically Allocated Memory for a 2 ArrayThe below C program illustrates how we can use the free() function to deallocate the memory of a dynamically allocated 2D array.
Output 2D array freed successfully Points to RememberThe following are the important points and common mistakes that we must keep in mind while deallocating the dynamic memory in C. 1. Double FreeingFreeing a pointer twice can lead to undefined behavior. Ensure that a pointer is not freed more than once. int *ptr = (int *)malloc(10 * sizeof(int)); 2. Freeing NULL PointerIt is safe to call free() with a NULL pointer. It has no effect. int *ptr = NULL; 3. Dangling PointersAfter freeing a pointer, it’s a good practice to set it to NULL to avoid dangling pointers, which point to freed memory. int *ptr = (int *)malloc(10 * sizeof(int)); 4. Freeing Pointers to Local VariablesDo not ever use the free() function on pointers to stack-allocated (local) variables. This will lead to undefined behavior. int x; 5. Exceptions
Related Articles
|
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
|
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |