共用方式為


_CrtIsValidHeapPointer

確認指定的指標是在某個 C 執行階段程式庫所配置的堆積中,但呼叫端的 CRT 程式庫不一定需要。 在 Visual Studio 2010 之前的 CRT 版本中,此函式會確認指定的指標位於本機堆積中(僅限偵錯版本)。

語法

int _CrtIsValidHeapPointer(
   const void *userData
);

參數

userData
所配置記憶體區塊開頭的指標。

傳回值

_CrtIsValidHeapPointer 如果指定的指標位於所有 CRT 程式庫實例共用的堆積中,則傳 TRUE 回 。 在 Visual Studio 2010 之前的 CRT 版本中,如果指定的指標位於本機堆積中,此函式會傳 TRUE 回 。 否則,此函式會傳回 FALSE

備註

不建議您使用此函式。 從 Visual Studio 2010 CRT 程式庫開始,所有 CRT 程式庫都會共用一個 OS 堆積 (「處理序堆積」)。 _CrtIsValidHeapPointer 函式會報告是否已在 CRT 堆積中配置指標,而不是由呼叫端的 CRT 程式庫配置它。 例如,請考慮使用 CRT 程式庫的 Visual Studio 2010 版本所配置的區塊。 _CrtIsValidHeapPointer如果 Visual Studio 2012 版本的 CRT 程式庫所匯出的函式會測試指標,則會傳 TRUE 回 。 此測試已不再有用。 在 Visual Studio 2010 之前的 CRT 程式庫版本中,此函式用來確定特定記憶體位址是在本機堆積中。 本機堆積指的是 C 執行階段程式庫的特定執行個體所建立和管理的堆積。 如果動態連結程式庫 (DLL) 包含與執行階段程式庫的靜態連結,則會有執行階段堆積的專屬執行個體,因此有其專屬堆積,而與應用程式的本機堆積無關。 未定義 時 _DEBUG ,會在前置處理期間移除 對 的 _CrtIsValidHeapPointer 呼叫。

因為此函式會傳 TRUE 回 或 FALSE ,所以可以傳遞至其中 _ASSERT 一個宏,以建立基本的偵錯錯誤處理機制。 如果指定的位址不在本機堆積內,下列範例會導致判斷提示失敗:

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

如需如何 _CrtIsValidHeapPointer 搭配其他偵錯函式和宏使用的詳細資訊,請參閱 報告 宏。 如需如何在基底堆積偵錯版本中配置、初始化及管理記憶體區塊的相關資訊,請參閱 CRT 偵錯堆積詳細資料

需求

常式 必要的標頭
_CrtIsValidHeapPointer <crtdbg.h>

如需相容性詳細資訊,請參閱相容性

程式庫

僅限偵錯版本的 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.

另請參閱

偵錯常式