/* MALLOC.C: This program allocates memory with * malloc, then frees the memory with free. */ #include /* Definition of _MAX_PATH */ #include #include 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" ); } /* #include For ANSI compatibility (malloc only) #include Required only for function declarations Syntax 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 ); malloc Standards: ANSI, UNIX 16-Bit: MS-DOS, QWIN, WIN, WIN DLL _bmalloc, _fmalloc, _nmalloc Standards: None 16-Bit: MS-DOS, QWIN, WIN, WIN DLL Parameter Description size Bytes to allocate seg Based heap segment selector 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: _brealloc fscanf _putw calloc fseek scanf _execl fsetpos _searchenv _execle _fullpath setvbuf _execlp fwrite _spawnl _execlpe getc _spawnle _execv getchar _spawnlp _execve _getcwd _spawnlpe _execvp _getdcwd _spawnv _execvpe gets _spawnve fgetc _getw _spawnvp _fgetchar _nrealloc _spawnvpe fgets _popen _strdup fprintf printf system fputc putc _tempnam _fputchar putchar ungetc fputs _putenv vfprintf fread puts vprintf _frealloc 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 retried allocating within the default data segment (that is, 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, 7.0, and this product. 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. */