C Library Function - free()
Description
In the C language,free() function is a standard library function used to free memory allocated by malloc(), calloc(), realloc() and other dynamic allocation functions. When dynamically allocated memory is no longer needed, calling the free() function can avoid memory leaks and ensure that the program manages memory efficiently.
Declaration
Below is the declaration of the free() function.
void free(void *ptr)
Parameters
- ptr -- The pointer points to a block of memory to be freed, which was previously allocated by calling malloc, calloc, or realloc. If the argument passed is a null pointer, no action is performed.
Return Value
This function does not return any value.
Example
The following example demonstrates the use of the free() function.
Example
#include
<
stdio.h
#include
<
stdlib.h
#include
<
string.h
int
main
(
)
{
char
str;
/*
Initial memory allocation
*/
str
=
(
char
)
malloc
(
15
)
;
strcpy
(
str
,
"
runoob
"
)
;
printf
(
"
String = %s, Address = %p
\
n
"
,
str
,
str
)
;
/*
Reallocating memory
*/
str
=
(
char
)
realloc
(
str
,
25
)
;
strcat
(
str
,
"
.com
"
)
;
printf
(
"
String = %s, Address = %p
\
n
"
,
str
,
str
)
;
/*
Freeing the allocated memory
*/
free
(
str
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
String = runoob, Address = 0x7fe4e4c02b10
String = runoob.com, Address = 0x7fe4e4c02b10
Notes
-
Freeing the correct memory: only memory allocated through dynamic memory allocation functions can be freed; memory allocated by other means (such as local or global variables) cannot be freed.
-
Avoid double freeing: the same block of memory cannot be freed more than once, otherwise undefined behavior may result.
-
Handling the pointer after freeing: after freeing memory, the pointer still points to the freed memory location. To avoid dangling pointers, the pointer can be set to
NULL.free(array);
array = NULL;
-
Check for a null pointer: Calling
free()it is best to check whether the pointer is null before calling it, to ensure program stability.if (array != NULL) { free(array); array = NULL;
}
By using the free() function correctly, dynamically allocated memory can be properly managed, avoiding memory leaks and dangling pointers, and improving program stability and reliability.
Memory leaks and dangling pointers
- Memory Leaks: if dynamically allocated memory is not freed or the reference to it is lost, the memory can no longer be used by the program, causing a memory leak.
- Dangling pointers: a pointer that points to freed memory is called a dangling pointer. If a dangling pointer is accessed again, it leads to undefined behavior, possibly causing a program crash or data corruption.
In C, after calling free() to release dynamically allocated memory, the pointer still holds the previously allocated address, which results in a dangling pointer. A dangling pointer is a pointer to freed memory; dereferencing it causes undefined behavior.
To avoid dangling pointer problems, the usual practice is to set the pointer to NULL after calling free(). This ensures the program does not accidentally use freed memory.
Example
#include
#include
int
main
(
)
{
// Dynamically allocate memory
char
str
=
(
char
)
malloc
(
100
sizeof
(
char
)
)
;
if
(
str
==
NULL
)
{
printf
(
"Memory allocation failed\n"
)
;
return
1
;
}
// Use the allocated memory
snprintf
(
str
,
100
,
"Hello, World!"
)
;
printf
(
"%s\n"
,
str
)
;
// Free the dynamically allocated memory
free
(
str
)
;
// Set the pointer to NULL to avoid a dangling pointer
str
=
NULL
;
// Now str is NULL, safely avoiding the dangling pointer problem
if
(
str
==
NULL
)
{
printf
(
"str is NULL\n"
)
;
}
return
0
;
}
In this example, the str pointer is set to NULL after the free() call, thus avoiding the dangling pointer problem. If you try to use this pointer again, since it is NULL, a conditional check can prevent undefined behavior.
C Standard Library -