内存中存储对象的正确决策(复合模式)

发布于 2024-10-16 17:23:07 字数 594 浏览 1 评论 0原文

我已经在 Main.cpp 中有 CCompositePrimitive 类的对象

int main()
{
    ...
    CCompositePrimitive DrawObjects;
    ...
}

CCompositePrimitive 类有 field:

private:
 list<CDrawObject*> m_Objects;

和 method:

public:
 Add(...);


void CCompositePrimitive::Add() {
 Objects.push_back(new Rectangle(...))
}

在一些论坛上问我 DrawObjects 对象将存储在堆中。但我认为不然。在我看来,DrawObjects 对象将存储在堆栈中。 如果 DrawObjects 可以存储在列表 m_Objects 中,我如何保留计算机的堆栈溢出内存(任何不同的内存问题);很多对象,也可以包含 CCompositePrimitive 对象(复合模式)。

I have already object of CCompositePrimitive class in Main.cpp

int main()
{
    ...
    CCompositePrimitive DrawObjects;
    ...
}

The CCompositePrimitive class has field:

private:
 list<CDrawObject*> m_Objects;

and method:

public:
 Add(...);


void CCompositePrimitive::Add() {
 Objects.push_back(new Rectangle(...))
}

On some forums ask me that DrawObjects object will be stored in heap. But I think else. In my opinion DrawObjects object will be stored in stack.
How I can preserve computer's memory of stack overflow (any different memory problems), if DrawObjects can store in list m_Objects; very many objects, which can content CCompositePrimitive objects too (composite pattern).

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

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

发布评论

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

评论(2

避讳 2024-10-23 17:23:07

取决于存储 CCompositePrimitive m_DrawOjects; 的类的创建方式。 (由于 m_,我假设它存储在类中)
如果您的类是在堆栈上创建的,那么 m_DrawObjects 也会如此。如果您的类是在堆上创建的,那么 m_DrawObjects 也将是在堆上创建的。
但是无论m_DrawObjects是在堆还是栈上创建的,list内的对象都可以被创建。 m_Objects; 将在堆上创建,因为这就是链表的工作原理。

编辑:根据您的编辑和注释,当然会在堆栈上创建DrawObjects。 :) 但我所说的关于列表中的对象仍然成立。

Depends on how the class where CCompositePrimitive m_DrawOjects; is stored in gets created. (I assume it's stored inside a class because of the m_)
If your class is created on the stack, then m_DrawObjects will be too. If your class is created on the heap, so will be m_DrawObjects.
But indifferent of wether m_DrawObjects is created on the heap or the stack, the objects inside the list<CDrawObject*> m_Objects; will be created on the heap, because that's how a linked list works.

Edit: According to your edit and comment, then DrawObjects is of course created on the stack. :) But what I said about the objects inside the list still holds true.

郁金香雨 2024-10-23 17:23:07

放入任何 STL 集合中的项目都是动态分配的。您的主要问题是管理集合中指针的生命周期。

Vector 将把指针存储在连续的存储中,因此每个项目使用的开销更少,因为它不需要指向下一个和上一个节点的指针。

当您插入或删除集合末尾以外的位置时,列表的效率更高,并且当您超出容量时,永远不会有重新分配的开销。

如果您不想产生重新分配的开销,拥有大量不需要连续存储的对象,并且只从两端插入和删除:任一端 - 但从不在中间,那么 deque 是最好的集合。

如果你想管理指针本身的生命周期,你可以使用shared_ptr作为你的集合类型。这也有一个开销。 boost 提供类型安全的指针集合来管理其中项目的内存。

Items you put into any STL collection are dynamically allocated. Your main issue is managing the lifetime of the pointers in the collection.

vector will store the pointers in contiguous storage and will therefore use less overhead per item as it does not need pointers to next and previous nodes.

list is more efficient in time when you are inserting or removing other than at the end of the collection, and will never have an overhead of reallocating when you grow beyond capacity.

deque is the best collection to use if you never want the overhead of reallocating, have large numbers of objects that do not need contiguous storage, and are only inserting and removing from the ends: either end - but never in the middle.

If you want to manage the lifetime of the pointers themselves you could use shared_ptr as your collection type. There is an overhead in that too. boost provides typesafe pointer-collections that manage the memory of the items in them.

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