C++ 如何检查无效指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/482315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:34:52  来源:igfitidea点击:

How do you check for an invalid pointer?

c++pointers

提问by DShook

My current code to the effect of:

我当前的代码效果如下:

if( objectPointer != NULL){
    delete objectPointer;
}

doesn't work because the pointers are getting set to invalid hex numbers by the compiler such as:

不起作用,因为编译器将指针设置为无效的十六进制数,例如:

  • 0xbaadf00d
  • 0xdeadbeef
  • 0xbaadf00d
  • 0x死牛肉

etc....

等等....

So what's the best way to check for an invalid pointer before trying to delete the object?

那么在尝试删除对象之前检查无效指针的最佳方法是什么?

回答by Brett Daniel

Always initialize your pointers to NULL (that is, 0). From http://www.lysator.liu.se/c/c-faq/c-1.html:

始终将指针初始化为 NULL(即 0)。来自http://www.lysator.liu.se/c/c-faq/c-1.html

A null pointer is conceptually different from an uninitialized pointer. A null pointer is known not to point to any object; an uninitialized pointer might point anywhere.

空指针在概念上不同于未初始化的指针。已知空指针不指向任何对象;未初始化的指针可能指向任何地方。

回答by 1800 INFORMATION

You don't need to check for not-NULL when calling delete. It is explicitly defined to do nothing.

调用 delete 时不需要检查 not-NULL。它被明确定义为什么都不做。

delete NULL; // this is allowed

Any correct code you are writing would not be affected by these weird values the compiler is putting into your uninitialised or already freed memory. It puts those values there in order to help you find bugs. Ergo, you have a bug.

您编写的任何正确代码都不会受到编译器放入未初始化或已释放内存的这些奇怪值的影响。它将这些值放在那里以帮助您找到错误。因此,你有一个错误。

回答by Johannes Schaub - litb

The best way is setting it to NULL if it doesn't point to anything. Globals, pointers in other namespaces, and local static pointers are automatically initialized to be null pointers. Class members and normal locals should be initialized to NULL manually if you need to test them against NULL (some people like to use 0instead. Of course, that's fully equivalent).

如果它不指向任何内容,最好的方法是将其设置为 NULL。全局变量、其他命名空间中的指针和本地静态指针会自动初始化为空指针。如果您需要针对 NULL 对类成员和普通本地人进行测试,则应手动将它们初始化为 NULL(有些人喜欢0改用。当然,这完全等效)。

Then, you can check against NULL, but also can pass the pointer right away to delete, because it won't have any effect to delete a null-pointer (guaranteed by the C++ Standard).

然后,您可以检查 NULL,但也可以立即将指针传递给delete,因为它不会对删除空指针产生任何影响(由 C++ 标准保证)。

回答by shoosh

You're asking the wrong question.
Your pointers should never be getting these values in the first place. you can't rely on the compiler to set an invalid pointer to something. you always need to do it yourself by assigning a NULL to it.

你问错了问题。
您的指针不应该首先获取这些值。您不能依靠编译器来设置指向某事的无效指针。你总是需要通过为它分配一个NULL来自己做。

回答by Mankarse

The best way to "check for an invalid pointer before trying to delete an object", is to never try to delete an object. All calls to deleteshould be performed in the destructors of objects that own the pointed-to data.

“在尝试删除对象之前检查无效指针”的最佳方法是永远不要尝试删除对象。所有对 的调用delete都应在拥有指向数据的对象的析构函数中执行。

The standard library is full of objects that do such ownership, and so you should almost never need to actually write one yourself. Try unique_ptr, vector, shared_ptror whichever other container suits your particular needs.

标准库中充满了拥有此类所有权的对象,因此您几乎不需要自己实际编写一个。尝试unique_ptrvectorshared_ptr或任何其他容器适合您的特定需求。

回答by DShook

found thisafter I had posted as well.

在我发帖后也发现了这个