Share via


_CrtIsValidPointer

Verifies that a specified memory range is valid for reading and writing (debug version only).

int_CrtIsValidPointer(constvoid *address**,unsignedintsize,int**access );

Routine Required Header Compatibility
_CrtIsValidPointer <crtdbg.h> Win NT, Win 95

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBCD.LIB Single thread static library, debug version
LIBCMTD.LIB Multithread static library, debug version
MSVCRTD.LIB Import library for MSVCRTD.DLL, debug version

Return Value

_CrtIsValidPointer returns TRUE if the specified memory range is valid for the specified operation(s); otherwise, the function returns FALSE.

Parameter

address

Points to the beginning of the memory range to test for validity

size

Size of the specified memory range (bytes)

access

Read/Write accessibility to determine for the memory range

Remarks

The _CrtIsValidPointer function verifies that the memory range beginning at address and extending for size bytes, is valid for the specified accessibility operation(s). When access is set to TRUE, the memory range is verified for both reading and writing. When address is FALSE, the memory range is only validated for reading. When _DEBUG is not defined, calls to _CrtIsValidPointer are removed during preprocessing.

Because this function returns TRUE or FALSE, it can be passed to one of the _ASSERT macros to create a simple debugging error handling mechanism. The following example will cause an assertion failure if the memory range is not valid for both reading and writing operations:

_ASSERTE( _CrtIsValidPointer( address, size, TRUE ) );

For more information about how _CrtIsValidPointer can be used with other debug functions and macros, see Using Macros for Verification and Reporting. For information about how memory blocks are allocated, initialized, and managed in the debug version of the base heap, see Memory Management and the Debug Heap.

Example

/*
 * 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

void 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.

Debug Functions