_CrtIsValidHeapPointer

指定したポインターが、必ずしも呼び出し元の CRT ライブラリではなく、任意の C ランタイム ライブラリによって割り当てられたヒープ内にあることを検証します。 Visual Studio 2010 より前のバージョンの CRT では、この関数は、指定したポインターがローカル ヒープ内にあることを確認します (デバッグ バージョンのみ)。

構文

int _CrtIsValidHeapPointer(
   const void *userData
);

パラメーター

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

戻り値

_CrtIsValidHeapPointer は、指定されたポインターがすべての CRT ライブラリ インスタンスによって共有されるヒープ内にある場合は TRUE を返します。 Visual Studio 2010 より前のバージョンの CRT では、この関数は、指定されたポインターがローカル ヒープ内にある場合に返 TRUE されます。 それ以外の場合、関数は FALSE を返します。

解説

この関数を使用することはお勧めしません。 Visual Studio 2010 CRT ライブラリ以降、すべての CRT ライブラリでは 1 つの OS ヒープ (プロセス ヒープ) を共有します。 _CrtIsValidHeapPointer 関数は、ポインターが CRT ヒープで割り当てられたかどうかを報告しますが、呼び出し元の CRT ライブラリによるものかどうかは報告しません。 たとえば、Visual Studio 2010 バージョンの CRT ライブラリを使用して割り当てられたブロックがあるとします。 Visual Studio 2012 バージョンの CRT ライブラリによってエクスポートされた _CrtIsValidHeapPointer 関数がポインターをテストする場合、TRUE を返します。 このテストは役に立たなくなりました。 Visual Studio 2010 以前のバージョンの CRT ライブラリでは、この関数は、特定のメモリ アドレスがローカル ヒープ内にあることを確認するために使用されます。 ローカル ヒープとは、C ランタイム ライブラリの特定のインスタンスによって作成および管理されるヒープを指します。 ダイナミック リンク ライブラリ (DLL) にランタイム ライブラリへの静的なリンクが含まれている場合、DLL はランタイム ヒープの独自のインスタンスを持つため、アプリケーションのローカル ヒープとは別の独自のヒープを持ちます。 _DEBUG が定義されていない場合、_CrtIsValidHeapPointer の呼び出しは前処理で削除されます。

この関数は返 TRUE すか、マクロ FALSEのいずれかに _ASSERT 渡して、基本的なデバッグ エラー処理メカニズムを作成できます。 次の例では、指定されたアドレスがローカル ヒープ内にない場合にアサーションの失敗が発生します。

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

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

必要条件

ルーチンによって返される値 必須ヘッダー
_CrtIsValidHeapPointer <crtdbg.h>

互換性の詳細については、「 Compatibility」を参照してください。

ライブラリ

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

次の例では、Visual Studio 2010 以前の C ランタイム ライブラリで使用される場合に、メモリが有効かどうかをテストする方法を示します。 この例は、従来の CRT ライブラリ コードのユーザー向けに提供されます。

// 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.

関連項目

デバッグ ルーチン