_CrtIsValidHeapPointer
確認指定的指標在本機堆積 (僅偵錯版本)。
int _CrtIsValidHeapPointer(
const void *userData
);
參數
- userData
要配置的記憶體區塊的開頭的指標。
傳回值
如果指定指標在本機堆積,_CrtIsValidHeapPointer 則傳回 TRUE。 否則,函式會傳回 false。
備註
_CrtIsValidHeapPointer 函式是用來確定特定記憶體位址在本機堆積內。 本機堆積參考 C 執行階段程式庫的特定執行個體建立和管理的堆積。 如果動態連結程式庫(DLL) 包含靜態連結到這個執行階段程式庫,它有執行階段堆積的執行個體,因此堆積,應用程式的本機堆積的影響。 如果未定義 _DEBUG,在前置處理中,對 _CrtIsValidHeapPointer 的呼叫將被移除。
由於這個函式會傳回 TRUE 或 FALSE,它可以傳遞至 _ASSERT 巨集之一,以建立一個簡單的偵錯錯誤處理機制。 如果指定的位址不在本機堆積中,尋找下列範例造成判斷提示失敗:
_ASSERTE( _CrtIsValidHeapPointer( userData ) );
如需 _CrtIsValidHeapPointer 如何與其他偵錯功能和巨集一起使用的詳細資訊,請參閱 報告巨集。 如需在基底堆積的記憶體區塊配置、初始化及管理的方式之偵錯版本的詳細資訊,請參閱 CRT 偵錯堆積詳細資料。
需求
常式 |
必要的標頭 |
---|---|
_CrtIsValidHeapPointer |
<crtdbg.h> |
如需更多關於相容性的資訊,請參閱入門介紹中的 相容性 (Compatibility) 。
程式庫
C run-time libraries 版本的偵錯
範例
// 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);
}
Output
my_pointer has read and write accessibility.
my_pointer is within the local heap.
.NET Framework 對等用法
不適用。若要呼叫標準 C 函式,請使用 PInvoke。如需詳細資訊,請參閱平台叫用範例。