_msize Functions

Description

Return the size of a memory block allocated in the heap.

#include <malloc.h> Required only for function declarations  

size_t _msize( void *memblock );

size_t _bmsize( __segment seg, void __based( void ) *memblock );

size_t _fmsize( void __far *memblock );

size_t _nmsize( void __near *memblock );

memblock Pointer to memory block  
seg Based-heap segment selector  

Remarks

The _msize family of functions returns the size, in bytes, of the memory block allocated by a call to the appropriate version of the calloc, malloc, or realloc functions.

In large data models (compact-, large-, and huge-model programs), _msize maps to _fmsize. In small data models (tiny-, small-, and medium-model programs), _msize maps to _nmsize.

The _nmsize function returns the size (in bytes) of the memory block allocated by a call to _nmalloc, and the _fmsize function returns the size (in bytes) of the memory block allocated by a call to _fmalloc or _frealloc. The _bmsize function returns the size of a block allocated in segment seg by a call to _bmalloc, _bcalloc, or _brealloc.

The location of the memory block is indicated below:

Function Data Segment

_msize Depends on data model of program
_bmsize Based heap segment specified by seg value
_fmsize Far heap segment (outside default data segment)
_nmsize Default data segment (inside near heap)

Return Value

All four functions return the size (in bytes) as an unsigned integer.

Compatibility

_msize

Standards:None

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

32-Bit:DOS32X

_bmsize, _fmsize, _nmsize

Standards:None

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

32-Bit:None

See Also

calloc functions, _expand functions, malloc functions, realloc functions

Example

/* REALLOC.C: This program allocates a block of memory for buffer

* and then uses _msize to display the size of that block. Next, it

* uses realloc to expand the amount of memory used by buffer

* and then calls _msize again to display the new amount of

* memory allocated to buffer.

*/

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

void main( void )

{

long *buffer;

size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )

exit( 1 );

size = _msize( buffer );

printf( "Size of block after malloc of 1000 longs: %u\n", size );

/* Reallocate and show new size: */

if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL )

exit( 1 );

size = _msize( buffer );

printf( "Size of block after realloc of 1000 more longs: %u\n", size );

free( buffer );

exit( 0 );

}

Output

Size of block after malloc of 1000 longs: 4000

Size of block after realloc of 1000 more longs: 8000