英文:
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'm defining a class having 2 and 3 D arrays that I'm trying to declare them as the `std::vector` rather than static arrays.
#include <iostream>
#include <vector>
#define MAX_DIVS 50
class Dummy
{
private:
std::vector<std::vector<int>> array;
//double m_param_st[MAX_DIVS + 1][MAX_DIVS + 1][2];
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;
}
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<Dummy*>`, I won'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<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))};
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论