Compartir a través de


_msize_dbg

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

size_t _msize_dbg( 
   void *userData, 
   int blockType  
);

Parameters

  • userData
    Pointer to the memory block for which to determine the size.

  • blockType
    Type of the specified memory block: _CLIENT_BLOCK or _NORMAL_BLOCK.

Return Value

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

Remarks

_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 CRT Debug Heap Details. For information about the allocation block types and how they are used, see Types of blocks on the debug heap. For information about the differences between calling a standard heap function and its debug version in a debug build of an application, see Debug Versions of Heap Allocation Functions.

This function validates its parameter. If memblock is a null pointer, _msize invokes an invalid parameter handler, as described in Parameter Validation. If the error is handled, the function sets errno to EINVAL and returns -1.

Requirements

Routine

Required header

_msize_dbg

<crtdbg.h>

For more compatibility information, see Compatibility in the Introduction.

Libraries

Debug versions of C run-time libraries only.

Example

// crt_msize_dbg.c
// compile with: /MTd
/*
 * 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, *newbuffer;
        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
         */
        newbuffer = _realloc_dbg( buffer, size + (40 * sizeof(long)), _NORMAL_BLOCK, __FILE__, __LINE__ );
        if( newbuffer == NULL )
               exit( 1 );
        buffer = newbuffer;
        size = _msize_dbg( buffer, _NORMAL_BLOCK );
        printf( "Size of block after _realloc_dbg of 40 more longs: %u\n", size );

        free( buffer );
        exit( 0 );
}

Output

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

.NET Framework Equivalent

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

See Also

Reference

Debug Routines

_malloc_dbg