Share via


_CrtIsValidHeapPointer

更新 : 2007 年 11 月

指定されたポインタがローカル ヒープにあることを検査します (デバッグ バージョンだけ)。

int _CrtIsValidHeapPointer( 
   const void *userData 
);

パラメータ

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

戻り値

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

解説

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

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

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

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

必要条件

ルーチン

必須ヘッダー

_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 を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

デバッグ ルーチン