测试不等于NULL的错误指针

来源:互联网 发布:高中语文知乎 编辑:程序博客网 时间:2024/05/18 19:46

AfxIsValidAddress 

Visual Studio 2005
Other Versions
1 out of 2 rated this helpful Rate this topic

Tests any memory address to ensure that it is contained entirely within the program's memory space.

BOOL AfxIsValidAddress(   const void* lp,   UINT nBytes,   BOOL bReadWrite = TRUE ); 

Parameters

lp

Points to the memory address to be tested.

nBytes

Contains the number of bytes of memory to be tested.

bReadWrite

Specifies whether the memory is both for reading and writing (TRUE) or just reading (FALSE).

Return Value

In debug builds, nonzero if the specified memory block is contained entirely within the program's memory space; otherwise 0.

In non-debug builds, nonzero if lp is not NULL; otherwise 0.

Remarks

The address is not restricted to blocks allocated by new.

Example

// Allocate a 5 character array, which should have a valid memory address.char *array = new char[5];// Create a null pointer, which should be an invalid memory address.char *null = (char *)0x0;bool Test1 = AfxIsValidAddress(array, 5);bool Test2 = AfxIsValidAddress(null, 5);
Test1 等于true  即为有效指针
Test2 等于false 即为无效指针
原创粉丝点击