Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Calculates the size of a block of memory in the heap (debug version only).

size_t _msize_dbg(
   void *userData,
   int blockType 


Pointer to the memory block for which to determine the size.
Type of the specified memory block: _CLIENT_BLOCK or _NORMAL_BLOCK.

Return Value

Upon successful completion, _msize_dbg returns the size (bytes) of the specified memory block, otherwise it returns NULL.


_msize_dbg is a debug version of the _msize function. When _DEBUG is not defined, each call to _msize_dbg is reduced to a call to _msize. Both _msize and _msize_dbg calculate the size of a memory block in the base heap, but _msize_dbg adds two debugging features: It includes the buffers on either side of the user portion of the memory block in the returned size, and it allows size calculations for specific block types.

For information about how memory blocks are allocated, initialized, and managed in the debug version of the base heap, see Memory Management and the Debug Heap. For information about the allocation block types and how they are used, see Types of Blocks on the Debug Heap. For information on the differences between calling a standard heap function versus its debug version in a debug build of an application, see Using the Debug Version Versus the Base Version.


Routine Required header Compatibility
_msize_dbg <crtdbg.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


Debug versions of C run-time libraries only.


// crt_reallocd.c
 * This program allocates a block of memory using _malloc_dbg
 * and then calls _msize_dbg to display the size of that block.
 * Next, it uses _realloc_dbg to expand the amount of
 * memory used by the buffer and then calls _msize_dbg again to
 * display the new amount of memory allocated to the buffer.

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <crtdbg.h>

int main( void )
        long *buffer;
        size_t size;

         * Call _malloc_dbg to include the filename and line number
         * of our allocation request in the header
        buffer = (long *)_malloc_dbg( 40 * sizeof(long), _NORMAL_BLOCK, __FILE__, __LINE__ );
        if( buffer == NULL )
               exit( 1 );

         * Get the size of the buffer by calling _msize_dbg
        size = _msize_dbg( buffer, _NORMAL_BLOCK );
        printf( "Size of block after _malloc_dbg of 40 longs: %u\n", size );

         * Reallocate the buffer using _realloc_dbg and show the new size
        buffer = _realloc_dbg( buffer, size + (40 * sizeof(long)), _NORMAL_BLOCK, __FILE__, __LINE__ );
        if( buffer == NULL )
               exit( 1 );
        size = _msize_dbg( buffer, _NORMAL_BLOCK );
        printf( "Size of block after _realloc_dbg of 40 more longs: %u\n", size );

        free( buffer );
        exit( 0 );


Size of block after _malloc_dbg of 40 longs: 160
Size of block after _realloc_dbg of 40 more longs: 320

See Also

Debug Functions | _malloc_dbg | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft