_CrtIsValidHeapPointer

验证指定的指针是在本地堆中(仅限调试版本)。

int _CrtIsValidHeapPointer(  
   const void *userData  
);

参数

  • userData
    指向一个分配的内存块的开始指针。

返回值

如果指定的指针在本地堆中,则**_CrtIsValidHeapPointer** 返回 TRUE。 否则,该函数返回 FALSE。

备注

使用 _CrtIsValidHeapPointer 函数来确保指定的内存地址在本地堆中。 本地堆引用 C 运行库的特定实例创建和托管的堆。 如果动态链接库 (DLL) 包含静态链接到 c 运行时库,它具有运行时堆其自己的堆,因此,其自己的堆独立于应用程序的本地堆。 当 _DEBUG 未定义时,在预处理期间移除对 _CrtIsValidHeapPointer 的调用。

因为函数返回 TRUE 或 FALSE, 所以能传递一个 _ASSERT 宏命令来创建一个简单的调试错误处理机制。 如果指定的地址不在本地堆内,则下面的示例将导致论断失败:

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

有关 _CrtIsValidHeapPointer 如何可用在其它调试函数和宏命令中的详细信息, 请参阅 用于报告的宏。 有关在调试版本中的基位置堆中内存如何分配,初始化和管理的详细信息,请参阅 CRT 调试堆详细信息

要求

例程

必需的标头

_CrtIsValidHeapPointer

<crtdbg.h>

有关更多兼容性信息,请参见“简介”中的兼容性

仅限 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);
}

Output

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

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见平台调用示例

请参见

参考

调试例程