Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - free()

C Library Function - free()

C Standard Library - <stdlib.h> C Standard Library -

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

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

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

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 - <stdlib.h> C Standard Library -