function
free
<cstdlib>
void free ( void * ptr );
Deallocate space in memory
A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.
If ptr does not point to a block of memory allocated with the above functions, the behavior is undefined.
If ptr is a null pointer, the function does nothing.
Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
/* free example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int * buffer1, * buffer2, * buffer3;
buffer1 = (int*) malloc (100*sizeof(int));
buffer2 = (int*) calloc (100,sizeof(int));
buffer3 = (int*) realloc (buffer2,500*sizeof(int));
free (buffer1);
free (buffer3);
return 0;
}
|
This program has no output. It just demonstrates some ways to allocate and free dynamic memory using the C stdlib functions.
See also
- malloc
- Allocate memory block (function
)
- calloc
- Allocate space for array in memory (function
)
- realloc
- Reallocate memory block (function
)