C++ std::queue::pop() 调用析构函数。什么是指针类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2002282/
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
C++ std::queue::pop() calls destructor. What of pointer types?
提问by San Jacinto
I have a std::queue
that is wrapped as a templated class to make a thread-safe queue. I have two versions of this class: one that stores value types, one that stores pointer types.
我有一个std::queue
包装为模板化的类来创建线程安全队列。我有这个类的两个版本:一个存储值类型,一个存储指针类型。
For the pointer type, I'm having trouble deleting the elements of the queue on destruction. The reason is that I don't know a way to remove the items from the queue safely.
对于指针类型,我无法在销毁时删除队列的元素。原因是我不知道如何安全地从队列中删除项目。
Thisreference states (vacuously, so I guess it doesn't actually STATE it) that the only way to remove elements from the queue is to call pop()
. The reference also says that pop()
calls the destructor for the item.
这个参考声明(空洞的,所以我猜它实际上并没有声明它)从队列中删除元素的唯一方法是调用pop()
. 参考文献还说pop()
调用该项目的析构函数。
Well, this causes problems with my pointer types because they may or may not actually point to aggregates. If one of them points to an aggregate, they all will, but because the wrapper is templated, there is no guarantee which type (aggregated or non-aggregated) we are dealing with.
好吧,这会导致我的指针类型出现问题,因为它们实际上可能指向也可能不指向聚合。如果其中一个指向一个聚合,他们都会,但是因为包装器是模板化的,所以不能保证我们正在处理哪种类型(聚合或非聚合)。
So, when pop()
calls the destructor, what happens? How do I ensure that everything is being removed and the memory deallocation properly?
那么,当pop()
调用析构函数时,会发生什么?如何确保所有内容都被删除并正确释放内存?
Lastly, my solution is using an older version of GCC for ARM9. I don't have control over this. I understand that there are libraries that have smart pointers and containers that would assist here, but they are off-limits for me.
最后,我的解决方案是为 ARM9 使用旧版本的 GCC。我对此没有控制权。我知道有些库具有智能指针和容器可以在这里提供帮助,但它们对我来说是禁区。
回答by Greg Hewgill
Pointers themselvesdon't actually have destructors, so calling pop()
on a queue containing a pointer won't call the destructor of the object your pointer points to.
指针本身实际上没有析构函数,因此调用pop()
包含指针的队列不会调用指针指向的对象的析构函数。
回答by Greg Hewgill
Online sources are worth what you pay for them - get a proper reference like Josuttis's book. pop() does not "call the destructor" - it simply removes an element from the queue adaptor's underlying representation (by default a std::deque) by calling pop_front() on it. If the thing being popped has a destructor, it will be used when the popped object goes out of scope, but the queue class has nothing to do with it.
在线资源物有所值 - 获得像Josuttis 的书这样的适当参考。pop() 不会“调用析构函数”——它只是通过调用 pop_front() 从队列适配器的底层表示(默认为 std::deque)中移除一个元素。如果被弹出的东西有析构函数,当弹出的对象超出作用域时会使用它,但队列类与它无关。
回答by Steve Jessop
"How do I ensure that everything is being removed and the memory deallocation properly?"
“我如何确保所有内容都被删除并正确释放内存?”
If you absolutely have to store pointers in your queue, and you want them to be automatically freed when they're pop
ed, then instead of a queue of pointers, you need a queue of objects which store a pointer, and delete it in their destructor. You could for example use a queue of shared_ptr. shared_ptr isn't in the standard library, but it's part of TR1 and is widely available.
如果您绝对必须将指针存储在您的队列中,并且您希望它们在被pop
编辑时自动释放,那么您需要一个存储指针的对象队列,而不是一个指针队列,并在它们的析构函数中删除它. 例如,您可以使用 shared_ptr 队列。shared_ptr 不在标准库中,但它是 TR1 的一部分并且广泛可用。
Otherwise, it's the responsibility of the caller to delete the object:
否则,删除对象是调用者的责任:
T *off = q.front();
q.pop();
delete off;
The summary is that containers of pointers to dynamically allocated objects are a bit awkward. If you can design your program so that containers store copies of your objects, instead of pointers to dynamic objects, then do so. Failing that, you're responsible for resource ownership, not the container. STL containers know nothing about ownership, they just copy and destroy their value_type
. Copying and destroying pointers does nothing to the objects they point to.
总结是动态分配对象的指针容器有点笨拙。如果你可以设计你的程序,让容器存储你的对象的副本,而不是指向动态对象的指针,那么就这样做。否则,您将负责资源所有权,而不是容器。STL 容器对所有权一无所知,它们只是复制和销毁它们的value_type
. 复制和销毁指针对它们指向的对象没有任何作用。