次の方法で共有


_CrtIsValidHeapPointer

指定したポインターがローカル ヒープ内にあることを検証します (デバッグ バージョンのみ)。

int _CrtIsValidHeapPointer(  
   const void *userData  
);

パラメーター

  • userData
    割り当てられたメモリ ブロックの先頭へのポインター。

戻り値

_CrtIsValidHeapPointer は、指定されたポインターがローカル ヒープ内にある場合は TRUE を返します。 それ以外の場合、関数は FALSE を返します。

解説

_CrtIsValidHeapPointer 関数は、特定のメモリ アドレスがローカル ヒープ内にあることを確認するために使用されます。 ローカル ヒープとは、C ランタイム ライブラリの特定のインスタンスによって作成および管理されるヒープを指します。 ダイナミック リンク ライブラリ (DLL) にランタイム ライブラリへの静的なリンクが含まれている場合、DLL はランタイム ヒープの独自のインスタンスを持つため、アプリケーションのローカル ヒープとは別の独自のヒープを持ちます。 _DEBUG が未定義の場合、_CrtIsValidHeapPointer の呼び出しはプリプロセスで削除されます。

この関数は TRUE または FALSE を返すため、_ASSERT 系マクロに渡すことによって、デバッグ用の単純なエラー処理機構を作成できます。 指定されたアドレスがローカル ヒープ内にない場合に、アサーションの失敗を発生させるには、次のように記述します。

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

_CrtIsValidHeapPointer を他のデバッグ関数およびマクロと共に使用する方法の詳細については、「レポート用マクロの使用」を参照してください。 デバッグ バージョンのベース ヒープに対するメモリ ブロックの割り当て、初期化、管理方法の詳細については、「CRT デバッグ ヒープ」を参照してください。

必要条件

ルーチン

必須ヘッダー

_CrtIsValidHeapPointer

<crtdbg.h>

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

ライブラリ

C ランタイム ライブラリのデバッグ バージョンのみ。

使用例

// crt_isvalid.c
/*
 * This program allocates a block of memory using _malloc_dbg
 * and then tests the validity of this memory by calling 
 * _CrtIsMemoryBlock,_CrtIsValidPointer, and _CrtIsValidHeapPointer.
 */

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

#define  TRUE   1
#define  FALSE  0

int main( void )
{
        char *my_pointer;

        /* 
         * Call _malloc_dbg to include the filename and line number
         * of our allocation request in the header information
         */
        my_pointer = (char *)_malloc_dbg( sizeof(char) * 10, 
        _NORMAL_BLOCK, __FILE__, __LINE__ );

        // Ensure that the memory got allocated correctly
        _CrtIsMemoryBlock((const void *)my_pointer, sizeof(char) * 10, 
        NULL, NULL, NULL );

         // Test for read/write accessibility
        if (_CrtIsValidPointer((const void *)my_pointer, 
        sizeof(char) * 10, TRUE))
                printf("my_pointer has read and write accessibility.\n");
        else
                printf("my_pointer only has read access.\n");

        // Make sure my_pointer is within the local heap
        if (_CrtIsValidHeapPointer((const void *)my_pointer))
                printf("my_pointer is within the local heap.\n");
        else
                printf("my_pointer is not located within the local"
                       " heap.\n");

        free(my_pointer);
}

出力

my_pointer has read and write accessibility.
my_pointer is within the local heap.

同等の .NET Framework 関数

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

参照

関連項目

デバッグ ルーチン