在 C++ 中测试 void 指针删除之前

发布于 2024-09-25 15:37:43 字数 326 浏览 6 评论 0原文

我在 C++ 中有一个数组:

Player ** playerArray;

它在它所在类的构造函数中初始化。

在析构函数中我有:

delete playerArray;

除了通过 Valgrind 测试程序时,它说有一些调用删除了 void 指针:

 operator delete(void*)

我想在调用delete之前测试playerArray是否为void指针以避免此错误。

有谁知道该怎么做?

I have an array in C++:

Player ** playerArray;

which is initialized in the constructor of the class it is in.

In the destructor I have:

delete playerArray;

except when testing the program through Valgrind it says that there are some calls to delete to a void pointer:

 operator delete(void*)

I want to test whether the playerArray is a void pointer before calling delete to avoid this error.

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逐鹿 2024-10-02 15:37:43

也许您的意思是删除[]playerArray。如果指针是数组而不是单个实例,则需要 []

Perhaps you meant delete [] playerArray. You need the [] if the pointer is an array, not a single instance.

温柔嚣张 2024-10-02 15:37:43

以下是删除运算符的定义方式。

void operator delete(void*) throw();
void operator delete[](void*) throw();

“operator delete”采用“void *”,因为指向任何对象的指针都可以转换为“void *”。

请注意,void 是不完整类型,因此不允许删除 void * 即

char *p = new char;
void *pv = p;
delete pv;            // not allowed

脚注 78:这意味着
不能使用删除对象
void* 类型的指针,因为 void 是
不是对象类型。

如果playerarray是指向玩家数组的指针,您很可能希望以不同的方式进行操作。 delete pplayer 不会执行您想要的操作。

Here's how operator delete is defined.

void operator delete(void*) throw();
void operator delete[](void*) throw();

'operator delete' takes a 'void *' since a pointer to any object can be converted to 'void *'.

Note that a void is an incomplete type and hence it is not allowed to delete a void * i.e

char *p = new char;
void *pv = p;
delete pv;            // not allowed

Footnote 78: This implies that an
object cannot be deleted using a
pointer of type void* because void is
not an object type.

In the case where playerarray is a pointer to an array of Players, you most likely want to do it differently. delete pplayer does not do what you want it to.

宣告ˉ结束 2024-10-02 15:37:43

我认为 valgrind 警告的是,删除是在这样的上下文中发生的:

int foo(void *mydata){
{
    SomeClass some_value = static_cast<SomeClass> mydata;
    some_value.dosomething();
    // now we're done with it
    delete mydata;
}

虽然转换很好,因为你碰巧知道 void 指针实际上是那种类型,所以你仍然在做一些可疑的事情,因为您正在删除 void 指针,而不是类型化指针。如果 SomeClass 是 POD,那可能没问题,但如果它有一些关键工作必须在其析构函数中完成,则该析构函数永远不会被调用。

I think what valgrind is warning about is that the delete is occuring in the context of something like this:

int foo(void *mydata){
{
    SomeClass some_value = static_cast<SomeClass> mydata;
    some_value.dosomething();
    // now we're done with it
    delete mydata;
}

although the cast is fine, since you happen to know that the void pointer is actually that type, you're still doing something fishy, because you're deleting the void pointer, rather than the typed pointer. If SomeClass is POD, that's probably ok, but if it has some critical work it must do in its destructor, that destructor never gets called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文