← All languages
C function of the day
Random

free

Deallocate a block of memory previously allocated.

Description

The free() function deallocates a block of memory previously allocated by malloc(), calloc(), or realloc(). After calling free(), the pointer becomes invalid and should not be dereferenced.

If ptr is NULL, free() does nothing. Passing a pointer that was not returned by a memory allocation function, or a pointer that has already been freed, results in undefined behavior.

It is good practice to set the pointer to NULL after freeing to prevent use-after-free bugs. free() does not return a value. It is declared in <stdlib.h>.

Arguments

NameDescriptionOptional
ptr Pointer to the memory block to deallocate. No

Example

free(arr);
arr = NULL;