deallocate a block of memory
#include <stdlib.h> /* For ANSI compatibility (free only) */ #include <malloc.h> /* Required for other function prototypes */ void free( void *ptr ); void _bfree( __segment seg, void __based(void) *ptr ); void _ffree( void __far *ptr ); void _nfree( void __near *ptr );
When the value of the argument ptr is NULL, the free() function does nothing; otherwise, the free() function deallocates the memory block located by the argument ptr, which points to a memory block previously allocated through a call to the appropriate version of calloc(), malloc() or realloc(). After the call, the freed block is available for allocation.
Each function deallocates memory from a particular heap, as listed below:
In a large data memory model, free() is equivalent to _ffree(); in a small data memory model, free() is equivalent to _nfree(). |
#include <stdio.h> #include <stdlib.h> void main() { char *buffer; buffer = (char *)malloc( 80 ); if( buffer == NULL ) { printf( "Unable to allocate memory\n" ); } else { /* rest of code goes here */ free( buffer ); /* deallocate buffer */ } }
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
calloc(), _expand(), halloc(), hfree(), malloc(), _msize(), realloc(), sbrk()