_CrtIsValidHeapPointer

验证指定的指针本地堆 (请只调试版本)。

int _CrtIsValidHeapPointer( 
   const void *userData 
);

参数

  • userData
    对分配的开头的指针内存块。

返回值

因此,如果指定的指针在本地堆,_CrtIsValidHeapPointer 返回 TRUE。 否则,该函数返回错误。

备注

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

由于此函数返回 TRUE 或 false,则可以通过某个 _ASSERT 宏创建一个简单的调试错误处理机制。 ,如果指定的地址不在本地堆内,该文件位于下面的示例生成断言失败:

_ASSERTE( _CrtIsValidHeapPointer( userData ) );

有关 _CrtIsValidHeapPointer 方式的更多信息可与其他调试函数和宏,请参见 使用验证或报告的宏。 有关如何的信息存储在基堆的调试版本中分配,初始化,并管理,请参见 内存管理和调试堆

要求

实例

必需的头

_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。有关更多信息,请参见 平台调用示例

请参见

参考

调试实例