在Visual C ++中,“Bad Ptr”是什么意思? [英] What are 'Bad Ptr' mean in Visual C++?

查看:1166
本文介绍了在Visual C ++中,“Bad Ptr”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C ++,当我们检查指针是否有效时,通常我们执行以下操作:

For C++, when we check whether the pointer is valid or not, usually we do the following:

if (p == NULL){
     // error handling
}

VC ++,即使 p 不为NULL(也称为0),它是 0x00000004 ,也意味着 Bad Ptr 。然后导致异常,我们知道这个地址是受保护的,不能被覆盖。

However, in VC++, even p is not NULL (aka 0), it is 0x00000004, also means Bad Ptr. and then cause exception, we know this address is protected, can not be overwritten.

我在这里搜索了类似的问题,但我没有得到答案。

I have searched similar questions here, but I did not get the answer.

我的问题是:


  1. 当Bad Ptr发生时,为什么不将此指针设置为0?

  2. 因为Bad Ptr的值不为零,如何在VC ++中检查指针是否有效?


推荐答案

在Windows中,虚拟内存地址空间(地址0x00000000到0x0000FFFF)中的第一个65536字节总是< >未映射,从不能访问RAM。一个功能,确保坏指针值总是崩溃与AccessViolation的程序。最常见的是因为一个NULL指针,但空指针不会产生对地址0的访问。在C ++中,试图通过一个空指针访问一个对象的成员将产生对大于0的地址的访问。

In Windows, the first 65536 bytes in the virtual memory address space (addresses 0x00000000 through 0x0000FFFF) are always unmapped, never being able to address RAM. A feature that ensures that bad pointer values always crashes the program with an AccessViolation. Most commonly because of a NULL pointer, but null pointers don't exclusive produce accesses to address 0. In C++, trying to access a member of a object through a null pointer will generate accesses to addresses larger than 0 for example.

这不是调试器停止的地方,你也会在其他地址看到Bad Ptr,这种类型引用未映射的内存页。如果取消引用,也会导致程序崩溃。不是这样的,0x00000004已经被底部的禁止区域覆盖。

That's not where the debugger stops, you will also see Bad Ptr on other addresses, the kind that reference unmapped memory pages. And, if dereferenced, will crash your program as well. Not your case, 0x00000004 is already covered by the exclusion zone at the bottom.

winapi有测试指针值是否不好的函数。我不打算记录它们,但这些功能很危险

The winapi has functions to test whether a pointer value is bad. I'm not going to document them though, these functions are dangerous.

这样的指针值总是 代码中的错误。您必须修复该错误。

Having a pointer value like that is always a bug in your code. You must fix the bug.

这篇关于在Visual C ++中,“Bad Ptr”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆