简体   繁体   English

如何在不进行动态内存分配的情况下将更多类的对象添加到向量中?

[英]How can I add more objects of a class to a vector without doing dynamic memory allocation?

class AClass
{
    // ...
}

~

class AnotherClass
{
    public:
        // ...
        void AMethod()
        {
            // ...
            AClass * ac = new AClass(); // Dynamic memory allocation here
            m_Objects.push_back(ac);
            // ...
        }
        // ...
    private:
        // ...
        std::vector<AClass *> m_Objects;
        // ...
}

I want to add new objects of AClass to the vector m_Objects . 我想将AClass新对象添加到向量m_Objects
Is there any other way of doing this without doing dynamic memory allocation? 如果不进行动态内存分配,还有其他方法吗?

There are two things causing dynamic memory allocations here: 这里有两点导致动态内存分配:

  1. vector::push_back
  2. new AClass()

Here is one idea how to reduce dynamic memory allocations. 这是一个减少动态内存分配的想法。

First step is to call m_Objects.reserve(N); 第一步是调用m_Objects.reserve(N); in the AnotherClass constructor, where N is the maximum number of AClass objects you want to keep at any given time. AnotherClass构造函数中,其中N是要在任何给定时间保留的AClass对象的最大数量。 This reserves enough memory to remove the need for dynamic allocations by vector::push_back . 这样可以保留足够的内存,从而无需使用vector::push_back进行动态分配。

Second step is to make m_Objects contain objects instead of pointers, ie make it type std::vector<AClass> instead of std::vector<AClass*> . 第二步是使m_Objects包含对象而不是指针,即使其键入std::vector<AClass>而不是std::vector<AClass*> This allows you to skip new and directly create the new object by growing the container: 这使您可以跳过new和通过发展集装箱直接创建新的对象:

m_Objects.resize(m_Objects.size() + 1);

Now, there will be no dynamic allocation when adding a new object. 现在,添加新对象时将没有动态分配。

If you mean "can I create new objects at runtime without doing dynamic allocation?" 如果您的意思是“我可以在运行时创建新对象而不进行动态分配吗?” then the answer is no. 那么答案是否定的。 That's what dynamic allocation is. 这就是动态分配。

If you want to create some before they're immediately needed by creating them in bulk ahead of time, then that's plausible. 如果您想通过立即批量创建它们来立即创建它们,那么这是合理的。 Simply allocate a large number and then pull them out of an array or vector as needed. 只需分配大量,然后根据需要将它们从数组或向量中拉出即可。

Not so long as m_Objects is a vector of pointers , unless you were to store a pointer to an automatic (stack) variable. 只要m_Objects指针vectorm_Objects可以了,除非您要存储指向自动(堆栈)变量的指针。 But that, undoubtedly, would be a very bad idea. 但这无疑是一个非常糟糕的主意。

There are only two ways to allocate objects at runtime: dynamically on the heap, and on the call stack. 在运行时只有两种分配对象的方法:在堆上动态分配和在调用堆栈上动态分配对象。

It is possible to instantiate multiple objects that are local (auto) variables within a function block by calling the function recursively, and adding these stack objects to your vector. 通过递归调用函数并将这些堆栈对象添加到向量中,可以实例化功能块内局部(自动)变量的多个对象。 But I'm pretty sure that's not what you really want. 但是我很确定那不是您真正想要的。

That means that the only other way is to use new to dynamically create the new objects. 这意味着唯一的另一种方法是使用new动态创建新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM