How to Deallocate Memory in C: Freeing Dynamically Allocated Memory
Learn how to effectively deallocate memory in C programming. Understand the importance of freeing dynamically allocated memory and how to use the free()
function to prevent memory leaks in your C applications.
Deallocate Memory in C
Deallocate (Free) Memory
When you no longer need a block of memory, it's essential to deallocate it, which is also referred to as "freeing" the memory. Dynamic memory remains reserved until you either deallocate it or the program ends.
Once the memory is freed, it can be used by other programs or may even be allocated to another part of your program.
Freeing Memory
To deallocate memory, you can use the free()
function as follows:
Syntax
free(pointer);
The pointer
parameter is a pointer to the address of the memory to be deallocated:
Example
#include
#include
int main() {
int *ptr;
ptr = malloc(sizeof(*ptr)); // Allocate memory for one integer
// If memory cannot be allocated, print a message and exit
if (ptr == NULL) {
printf("Unable to allocate memory");
return 1;
}
// Set the value of the integer
*ptr = 20;
// Print the integer value
printf("Integer value: %d\n", *ptr);
// Free allocated memory
free(ptr);
// Set the pointer to NULL to prevent accidental use
ptr = NULL;
return 0;
}
Output
Integer value: 20
Memory Leaks
A memory leak occurs when dynamic memory is allocated but never freed. This can lead to excessive memory usage, causing your computer to slow down, especially if it happens in a loop or frequently called function.
It's crucial to keep track of pointers to dynamic memory to avoid losing access to them, which can result in memory leaks. Here are some examples of how a pointer to dynamic memory can be lost:
Example 1: Pointer Overwritten
In this example, the pointer is overwritten:
Example
int x = 5;
int *ptr;
ptr = calloc(2, sizeof(*ptr)); // Allocate memory
ptr = &x; // Pointer is now pointing to x
// Memory allocated by calloc() is lost
Example 2: Pointer Exists Only Inside a Function
This example demonstrates that if a pointer is created inside a function, it may not be accessible afterward:
Example
#include
#include
void myFunction() {
int *ptr;
ptr = malloc(sizeof(*ptr)); // Memory allocated
// Memory remains allocated after function ends but cannot be accessed
}
int main() {
myFunction();
printf("The function has ended");
return 0;
}
Example 3: Pointer Lost During Reallocation Failure
This example illustrates what happens if the reallocation fails:
Example
int* ptr;
ptr = malloc(sizeof(*ptr)); // Allocate memory
ptr = realloc(ptr, 2 * sizeof(*ptr)); // Attempt to reallocate memory
// If realloc() fails, the original pointer is lost
Summary
In summary, when managing memory in C, adhere to best practices:
- Always check for errors (NULL return values) to determine if memory allocation was successful.
- Prevent memory leaks by freeing memory that is no longer in use; otherwise, your program might underperform or crash due to lack of memory.
- Set the pointer to NULL after freeing memory to avoid accidental use.