你如何检查无效指针?

时间:2022-06-29 21:22:24

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

etc....

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

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

6 个解决方案

#1


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.

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

#2


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.

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

#3


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 0 instead. Of course, that's fully equivalent).

如果它没有指向任何东西,最好的方法是将其设置为NULL。 Globals,其他命名空间中的指针和本地静态指针会自动初始化为空指针。如果需要针对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,但也可以立即传递指针进行删除,因为它对删除空指针没有任何影响(由C ++标准保证)。

#4


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来自己做。

#5


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 delete should be performed in the destructors of objects that own the pointed-to data.

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

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_ptr or whichever other container suits your particular needs.

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

#6


found this after I had posted as well.

在我发布之后发现了这个。

#1


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.

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

#2


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.

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

#3


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 0 instead. Of course, that's fully equivalent).

如果它没有指向任何东西,最好的方法是将其设置为NULL。 Globals,其他命名空间中的指针和本地静态指针会自动初始化为空指针。如果需要针对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,但也可以立即传递指针进行删除,因为它对删除空指针没有任何影响(由C ++标准保证)。

#4


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来自己做。

#5


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 delete should be performed in the destructors of objects that own the pointed-to data.

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

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_ptr or whichever other container suits your particular needs.

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

#6


found this after I had posted as well.

在我发布之后发现了这个。