_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 為 Null 指標, _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