Share via


_msize_dbg

更新 : 2007 年 11 月

ヒープのメモリ ブロックのサイズを計算します (デバッグ バージョンのみ)。

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_dbg 関数も _msize 関数と同様にベース ヒープのメモリ ブロックのサイズを計算しますが、_msize_dbg 関数には 2 つのデバッグ機能があります。これらのデバッグ機能によって、メモリ ブロックのユーザー領域の両端のバッファを戻り値に含めたり、特定の型のブロックのサイズを計算したりできます。

デバッグ バージョンのベース ヒープに対するメモリ ブロックの割り当て、初期化、管理方法の詳細については、「メモリ管理とデバッグ ヒープ」を参照してください。割り当てブロック型とその使用方法については、「デバッグ ヒープ上のメモリ ブロックの型」を参照してください。標準で呼び出すヒープ関数と、アプリケーションのデバッグ ビルドで呼び出すデバッグ バージョンのヒープ関数との違いの詳細については、「デバッグ バージョンのヒープ割り当て関数」を参照してください。

この関数は、パラメータを検証します。memblock が null ポインタの場合、_msize は、「パラメータの検証」に説明されているように、無効なパラメータ ハンドラを呼び出します。エラーが処理されると、この関数は errnoEINVAL に設定し、-1 を返します。

必要条件

ルーチン

必須ヘッダー

_msize_dbg

<crtdbg.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

ライブラリ

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

.NET Framework の相当するアイテム

適用できません。標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

デバッグ ルーチン

_malloc_dbg