BOOL WINAPI IsBadWritePtr(
_In_ LPVOID LP,
_In_ UINT_PTR UCB
); |
//----------------------------------------------------------------------------
//这是一个测试1
char *pbuf1 = new char[10];
pbuf1 = (char *)0x0; //经过这里赋值 pbuf现在是一个错误的指针
bool br1 = IsBadWritePtr(pbuf1,10); //返回的值是true
if(!br1)
{
memcpy(pbuf1,"111111111",10);
cout << pbuf1 << endl;
printf("Display1:%s \n",pbuf1);
}
//这是一个测试2
char *pbuf2 = new char[10];
bool br2 = IsBadWritePtr(pbuf2,10); //返回的值是true
if(!br2)
{
memcpy(pbuf2,"222222222",10);
printf("Display2:%s \n",pbuf2);
}
//输出结果
//Display2:222222222
//----------------------------------------------------------------------------
MFC里也可以使用AfxIsValidAddress方法判断。
// 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;
ASSERT(AfxIsValidAddress(array, 5));
ASSERT(!AfxIsValidAddress(null, 5)); |
原文地址:http://www.cnblogs.com/lidabo/p/3708671.html