malloc Functions

Description

Allocate memory blocks.

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

void *malloc( size_t size );

void __based(void) *_bmalloc( __segment seg, size_t size );

void __far *_fmalloc( size_t size );

void __near *_nmalloc( size_t size );

size Bytes to allocate  
seg Based heap segment selector  

Remarks

Functions in the malloc family allocate a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information. If size is 0, each of these functions allocates a zero-length item in the heap and returns a valid pointer to that item.

The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

In large data models (compact-, large-, and huge-model programs), malloc maps to _fmalloc. In small data models (tiny-, small-, and medium-model programs), malloc maps to _nmalloc. The _fmalloc function allocates a memory block of at least size bytes in the far heap, which is outside the default data segment.

The _bmalloc function allocates a memory block of at least size bytes in the based heap segment specified by the segment selector seg.

The malloc functions allocate memory in the heap segment specified below:

Function Heap Segment

malloc Depends on data model of program
_bmalloc Based heap segment specified by seg value
_fmalloc Far heap (outside default data segment)
_nmalloc Near heap (within default data segment)

The functions listed below call the malloc family of routines. In addition, the startup code uses malloc to allocate storage for the environ/envp and argv strings and arrays.

The following routines call malloc:

calloc_execv_execve_execvp_execvpe_execl_execle_execlp_execlpefgetc_fgetchar fgetsfprintffputc_fputcharfputsfreadfscanffseekfsetpos_fullpathfwritegetcgetchar_getcwd_getdcwdgets_getw_popenprintfputcputchar_putenvputs_putw_searchenv_spawnv_spawnve_spawnvp_spawnvpe_spawnl_spawnle_spawnlp_spawnlpe_strdupsystemscanfsetvbuf_tempnamungetcvfprintfvprintf

The following routines call _nmalloc:

_nrealloc
_ncalloc
_nstrdup
realloc (in small data models)

The following routines call _fmalloc:

_frealloc
_fcalloc

_fstrdup
realloc (in large data models)

In Microsoft C version 5.1, the _fmalloc function would retry allocating within the default data segment (i.e., in the near heap) if sufficient memory was not available outside the default data segment. Since version 6.0, _fmalloc returns NULL under these conditions.

The _freect, _memavl, and _memmax functions called malloc in Microsoft C version 5.1 but do not do so in versions 6.0 and 7.0.

Return Value

The malloc function returns a void pointer to the allocated space. The _nmalloc function returns a ( void __near * ) and _fmalloc returns a ( void __far * ). The _bmalloc function returns a ( void __based( void ) * ).

The _malloc, _fmalloc, and _nmalloc functions return NULL if there is insufficient memory available. The _bmalloc function returns _NULLOFF if there is insufficient memory available.

Always check the return from the malloc function, even if the amount of memory requested is small.

Compatibility

malloc

Standards:ANSI, UNIX

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

32-Bit:DOS32X

_bmalloc, _fmalloc, _nmalloc

Standards:None

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

32-Bit:None

See Also

calloc functions, free 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 pathname\n" );

free( string );

printf( "Memory freed\n" );

}

Output

Memory space allocated for pathname

Memory freed