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

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

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

问题

  1. 我正在定义一个包含2维和3维数组的类,我试图将它们声明为`std::vector`而不是静态数组。
  2. ```cpp
  3. #include <iostream>
  4. #include <vector>
  5. #define MAX_DIVS 50
  6. class Dummy
  7. {
  8. private:
  9. std::vector<std::vector<int>> array;
  10. std::vector<std::vector<std::vector<double>>> m_param_st;
  11. public:
  12. Dummy()
  13. {
  14. array.resize(MAX_DIVS + 1);
  15. for (auto i = 0; i < MAX_DIVS + 1; i++)
  16. {
  17. array[i].resize(MAX_DIVS + 1);
  18. }
  19. m_param_st.resize(MAX_DIVS + 1);
  20. for (auto i = 0; i < MAX_DIVS + 1; i++)
  21. {
  22. m_param_st[i].resize(MAX_DIVS + 1);
  23. for (auto j = 0; j < MAX_DIVS + 1; j++)
  24. {
  25. m_param_st[i][j].resize(2);
  26. }
  27. }
  28. }
  29. ~Dummy(){};
  30. };
  31. int main(int argc, char *const argv[])
  32. {
  33. std::size_t size = 100000;
  34. std::vector<Dummy> Temp1(size, Dummy());
  35. return 0;
  36. }

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. #include &lt;iostream&gt;
  5. #include &lt;vector&gt;
  6. #define MAX_DIVS 50
  7. class Dummy
  8. {
  9. private:
  10. std::vector&lt;std::vector&lt;int&gt;&gt; array;
  11. //double m_param_st[MAX_DIVS + 1][MAX_DIVS + 1][2];
  12. std::vector&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; m_param_st;
  13. public:
  14. Dummy()
  15. {
  16. array.resize(MAX_DIVS+1);
  17. for(auto i=0 ; i&lt;MAX_DIVS+1 ; i++)
  18. {
  19. array[i].resize(MAX_DIVS+1);
  20. }
  21. m_param_st.resize(MAX_DIVS + 1);
  22. for (auto i = 0; i &lt; MAX_DIVS + 1; i++)
  23. {
  24. m_param_st[i].resize(MAX_DIVS + 1);
  25. for (auto j = 0; j &lt; MAX_DIVS + 1; j++)
  26. {
  27. m_param_st[i][j].resize(2);
  28. }
  29. }
  30. }
  31. ~Dummy() {};
  32. };
  33. int main(int argc, char* const argv[])
  34. {
  35. std::size_t size = 100000;
  36. std::vector&lt;Dummy&gt; Temp1 (size, Dummy() );
  37. return 0;
  38. }
  39. 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.
  40. 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`.
  41. </details>
  42. # 答案1
  43. **得分**: 1
  44. Oh, I see some funny code here! 🤖🚀
  45. Here's the translated code snippet:
  46. ```cpp
  47. class Dummy
  48. {
  49. private:
  50. std::vector<std::vector<int>> array{MAX_DIVS + 1, std::vector<int>(MAX_DIVS + 1)};
  51. std::vector<std::vector<std::vector<double>>> m_param_st{MAX_DIVS + 1,
  52. std::vector<std::vector<double>>(MAX_DIVS + 1, std::vector<double>(2))};
  53. };

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.

  1. class Dummy
  2. {
  3. private:
  4. std::vector&lt;std::vector&lt;int&gt;&gt; array{MAX_DIVS + 1, std::vector&lt;int&gt;(MAX_DIVS + 1)};
  5. std::vector&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; m_param_st{MAX_DIVS + 1,
  6. std::vector&lt;std::vector&lt;double&gt;&gt;(MAX_DIVS + 1, std::vector&lt;double&gt;(2))};
  7. };

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:

确定