How to properly allocate memory for std::vector of a class having relatively large array as member

huangapple go评论119阅读模式
英文:

How to properly allocate memory for std::vector of a class having relatively large array as member

问题

我正在定义一个包含2维和3维数组的类,我试图将它们声明为`std::vector`而不是静态数组。

```cpp
#include <iostream>
#include <vector>
#define MAX_DIVS 50

class Dummy
{
private:
    std::vector<std::vector<int>> array;
    std::vector<std::vector<std::vector<double>>> m_param_st;

public:
    Dummy()
    {
        array.resize(MAX_DIVS + 1);
        for (auto i = 0; i < MAX_DIVS + 1; i++)
        {
            array[i].resize(MAX_DIVS + 1);
        }

        m_param_st.resize(MAX_DIVS + 1);
        for (auto i = 0; i < MAX_DIVS + 1; i++)
        {
            m_param_st[i].resize(MAX_DIVS + 1);
            for (auto j = 0; j < MAX_DIVS + 1; j++)
            {
                m_param_st[i][j].resize(2);
            }
        }
    }
    ~Dummy(){};
};

int main(int argc, char *const argv[])
{
    std::size_t size = 100000;
    std::vector<Dummy> Temp1(size, Dummy());
    return 0;
}

后来,我需要拥有我的对象的std::vector,但当对象数量达到100000时出现问题(明显地,崩溃取决于可用堆栈大小,不同系统可能不同),有时会引发std::bad_alloc异常。

显然,如果我定义std::vector<Dummy*>,我就不会有这个问题,但是我有一些限制,不能这样做,只能在std::vector内部进行静态对象分配。


<details>
<summary>英文:</summary>

I&#39;m defining a class having 2 and 3 D arrays that I&#39;m trying to declare them as the `std::vector` rather than static arrays. 

    #include &lt;iostream&gt;
    #include &lt;vector&gt;
    #define MAX_DIVS 50
    
    class Dummy
    {
    private:
    	std::vector&lt;std::vector&lt;int&gt;&gt; array; 
    
    	//double m_param_st[MAX_DIVS + 1][MAX_DIVS + 1][2];
    	std::vector&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; m_param_st;
    public:
    	Dummy() 
    	{
    		array.resize(MAX_DIVS+1);
            for(auto i=0 ; i&lt;MAX_DIVS+1 ; i++)
            {
    			array[i].resize(MAX_DIVS+1); 
            }
    		
    		m_param_st.resize(MAX_DIVS + 1);
            for (auto i = 0; i &lt; MAX_DIVS + 1; i++)
            {
                m_param_st[i].resize(MAX_DIVS + 1);
                for (auto j = 0; j &lt; MAX_DIVS + 1; j++)
                {
                    m_param_st[i][j].resize(2);
                }
            }
        }
    	~Dummy() {}; 
    };
    
    
    int main(int argc, char* const argv[]) 
    {
     	
    	std::size_t size = 100000;  
      	std::vector&lt;Dummy&gt; Temp1 (size, Dummy() ); 
    	return 0; 
    }

later, I need to have `std::vector` of my objects, but it hangs up for only 100000 (definitely, at which point crashes depend on available stack size and it varies system by system) of objects and sometimes, it throws std::bad_alloc. 

Obviously, if instead I define `std::vector&lt;Dummy*&gt;`, I won&#39;t have this problem, but I have constraints not to do that and only have static allocation of objects inside `std::vector`.

</details>


# 答案1
**得分**: 1

Oh, I see some funny code here! 🤖🚀

Here's the translated code snippet:

```cpp
class Dummy
{
private:
    std::vector<std::vector<int>> array{MAX_DIVS + 1, std::vector<int>(MAX_DIVS + 1)}; 
    std::vector<std::vector<std::vector<double>>> m_param_st{MAX_DIVS + 1, 
        std::vector<std::vector<double>>(MAX_DIVS + 1, std::vector<double>(2))};
};

No need for any explicit constructors or destructors, just some playful code to tinker with! 😄✨

英文:

You don't need an explicit constructor and a destructor.

class Dummy
{
private:
    std::vector&lt;std::vector&lt;int&gt;&gt; array{MAX_DIVS + 1, std::vector&lt;int&gt;(MAX_DIVS + 1)}; 
    std::vector&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; m_param_st{MAX_DIVS + 1, 
            std::vector&lt;std::vector&lt;double&gt;&gt;(MAX_DIVS + 1, std::vector&lt;double&gt;(2))};
};

huangapple
  • 本文由 发表于 2023年7月17日 11:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定