_msize_dbg

计算堆(仅限调试版本)中内存块的大小。

语法

size_t _msize_dbg(
   void *userData,
   int blockType
);

参数

userData
指向可确定大小的内存块的指针。

blockType
指定的内存块的类型:_CLIENT_BLOCK_NORMAL_BLOCK

返回值

如果成功完成,_msize_dbg 返回指定内存块的大小(以字节为单位);否则返回 NULL

注解

_msize_dbg 是 _msize 函数的调试版本。 未定义 _DEBUG 时,每个对 _msize_dbg 的调用都会减少到对 _msize 的调用。 _msize_msize_dbg 都计算基堆中的内存块的大小,但 _msize_dbg 增加了两个个调试功能:它在返回大小的内存块用户部分的任一侧包括缓冲区,并且允许对指定块类型进行大小计算。

若要了解如何在基堆的调试版本中分配、初始化和管理内存块,请参阅 CRT 调试堆详细信息。 若要了解分配块类型及其使用方式,请参阅调试堆上的块类型。 有关标准堆函数与调试版本之间的差异的信息,请参阅堆分配函数的调试版本

此函数验证其参数。 如果 memblock 为空指针,则 _msize_dbg 将调用无效参数处理程序,如参数验证中所述。 如果处理了错误,则该函数将 errno 设置为 EINVAL 并返回 -1。

要求

例程 必需的标头
_msize_dbg <crtdbg.h>

有关兼容性的详细信息,请参阅 兼容性

仅限 C 运行时库的调试版本。

示例

// 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 );
}

输出

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

另请参阅

调试例程
_malloc_dbg