free(), _bfree(), _ffree(), _nfree()

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:

Depends on data model of the program (see below)
Based heap specified by seg value
Far heap (outside the default data segment)
Near heap (inside the default data segment)

Note: 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 */


free() is ANSI; _bfree(), _ffree(), and _nfree() are WATCOM.
Interrupt handler No
Signal handler No
Thread Yes

See also:

calloc(), _expand(), halloc(), hfree(), malloc(), _msize(), realloc(), sbrk()