Defined in header <stdlib.h> | ||
|---|---|---|
void free( void* ptr ); |
Deallocates the space previously allocated by malloc(), calloc(), aligned_alloc(), (since C11) or realloc().
If ptr is a null pointer, the function does nothing.
The behavior is undefined if the value of ptr does not equal a value returned earlier by malloc(), calloc(), realloc(), or aligned_alloc() (since C11).
The behavior is undefined if the memory area referred to by ptr has already been deallocated, that is, free() or realloc() has already been called with ptr as the argument and no calls to malloc(), calloc(), realloc(), or aligned_alloc() (since C11) resulted in a pointer equal to ptr afterwards.
The behavior is undefined if after free() returns, an access is made through the pointer ptr (unless another allocation function happened to result in a pointer value equal to ptr).
|
A call to | (since C11) |
| ptr | - | pointer to the memory to deallocate |
(none).
The function accepts (and does nothing with) the null pointer to reduce the amount of special-casing. Whether allocation succeeds or not, the pointer returned by an allocation function can be passed to free().
#include <stdlib.h>
int main(void)
{
int *p1 = malloc(10*sizeof *p1);
free(p1); // every allocated pointer must be freed
int *p2 = calloc(10, sizeof *p2);
int *p3 = realloc(p2, 1000*sizeof *p3);
if(p3) // p3 not null means p2 was freed by realloc
free(p3);
else // p3 null means p2 was not freed
free(p2);
}| allocates memory (function) |
|
C++ documentation for free |
|
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/c/memory/free