Deallocate a block of memory previously allocated.
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>.
| Name | Description | Optional |
|---|---|---|
ptr |
Pointer to the memory block to deallocate. | No |
free(arr);
arr = NULL;