C++ std::queue::pop() 调用析构函数。指针类型又如何呢?

发布于 2024-08-16 09:24:29 字数 575 浏览 3 评论 0原文

我有一个 std::queue ,它被包装为模板类以创建线程安全队列。我有这个类的两个版本:一种存储值类型,一种存储指针类型。

对于指针类型,我在销毁时删除队列元素时遇到问题。原因是我不知道如何安全地从队列中删除项目。

This 参考文献指出(空洞,所以我猜它实际上并没有陈述)从队列中删除元素的唯一方法是调用 pop()。该参考文献还指出 pop() 调用该项目的析构函数。

好吧,这会导致我的指针类型出现问题,因为它们实际上可能指向聚合,也可能不指向聚合。如果其中之一指向聚合,那么它们都会指向聚合,但由于包装器是模板化的,因此无法保证我们正在处理哪种类型(聚合或非聚合)。

那么,当 pop() 调用析构函数时,会发生什么?如何确保所有内容都被删除并正确释放内存?

最后,我的解决方案是使用适用于 ARM9 的旧版本 GCC。我无法控制这个。我知道有些库拥有智能指针和容器,可以在这里提供帮助,但它们对我来说是禁区。

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.

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.

This reference 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.

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?

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.

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

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

发布评论

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

评论(3

指针本身实际上没有析构函数,因此在包含指针的队列上调用 pop() 不会调用指针指向的对象的析构函数。

Pointers themselves don'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.

梦初启 2024-08-23 09:24:29

在线资源物有所值 - 获取适当的参考资料,例如 Josuttis 的书。 pop() 不会“调用析构函数” - 它只是通过调用 pop_front() 从队列适配器的底层表示(默认情况下为 std::deque)中删除一个元素。如果被弹出的对象有一个析构函数,当弹出的对象超出范围时,它将被使用,但队列类与它无关。

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.

z祗昰~ 2024-08-23 09:24:29

“我如何确保所有内容都被删除并且内存重新分配正确?”

如果您绝对必须在队列中存储指针,并且希望它们在弹出时自动释放,那么您需要一个存储对象的队列,而不是指针队列。指针,并在其析构函数中将其删除。例如,您可以使用shared_ptr 队列。 shared_ptr 不在标准库中,但它是 TR1 的一部分并且广泛可用。

否则,调用者有责任删除该对象:

T *off = q.front();
q.pop();
delete off;

总而言之,指向动态分配对象的指针容器有点尴尬。如果您可以设计程序,以便容器存储对象的副本,而不是指向动态对象的指针,那么就这样做。否则,您将负责资源所有权,而不是容器。 STL 容器对所有权一无所知,它们只是复制并销毁其 value_type。复制和销毁指针对它们指向的对象没有任何作用。

"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 poped, 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.

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.

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