free Functions

Description

Deallocate a memory block.

#include <stdlib.h> For ANSI compatibility (free only)  
#include <malloc.h> Required only for function declarations  

void free( void *memblock );

void _bfree( __segment seg, void __based( void ) *memblock );

void _ffree( void __far *memblock );

void _nfree( void __near *memblock );

memblock Allocated memory block  
seg Based-heap segment selector  

Remarks

The free family of functions deallocates a memory block. The argument memblock points to a memory block previously allocated through a call to calloc, malloc, or realloc. The number of bytes freed is the number of bytes specified when the block was allocated (or reallocated, in the case of realloc). After the call, the freed block is available for allocation.

The seg argument specifies the based heap containing the memory block to be freed by the _bfree function.

Attempting to free an invalid pointer may affect subsequent allocation and cause errors. An invalid pointer is one not allocated with the appropriate call.

The following restrictions apply to use of the free, _bfree, _ffree, and _nfree functions:

Blocks allocated with: Should be freed with:

calloc, malloc, realloc free
_bcalloc, _bmalloc, _brealloc _bfree
_fcalloc, _fmalloc, _frealloc _ffree
_ncalloc, _nmalloc, _nrealloc _nfree

A NULL pointer argument is ignored.

In large data models (compact-, large-, and huge-model programs), free maps to _ffree. In small data models (tiny-, small-, and medium-model programs), free maps to _nfree.

The various free functions deallocate a memory block in the segments shown in the list below:

Function Data Segment

free Depends on data model of program
_bfree Based heap specified by seg value
_ffree Far heap (outside default data segment)
_nfree Near heap (inside default data segment)

Return Value

None.

Compatibility

free

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_bfree, _ffree, _nfree

Standards:None

16-Bit:DOS, WIN, WIN DLL

32-Bit:None

See Also

calloc functions, malloc functions, realloc functions

Example

/* MALLOC.C: This program allocates memory with malloc, then frees

* the memory with free.

*/

#include <stdlib.h> /* Definition of _MAX_PATH */

#include <stdio.h>

#include <malloc.h>

void main( void )

{

char *string;

/* Allocate space for a path name */

string = malloc( _MAX_PATH );

if( string == NULL )

printf( “Insufficient memory available\n” );

else

printf( “Memory space allocated for path name\n” );

free( string );

printf( “Memory freed\n” );

}

Output

Memory space allocated for path name

Memory freed