我有一个体素结构:
struct voxel
{
unsigned char R, G, B;
voxel()
{
R = G = B = 0;
}
//parameteric contructor with parameters
voxel(unsigned char pR, unsigned char pG, unsigned char pB)
{
R = pR; G = pG; B = pB;
}
};
Run Code Online (Sandbox Code Playgroud)
对于体素的数量,我有一个非常大的n.
int n = 300 * 300 * 300;
Run Code Online (Sandbox Code Playgroud)
现在,当我用向量初始化体素时,RAM大约需要79 MB.
std::vector< voxel > vi(n);
Run Code Online (Sandbox Code Playgroud)
但是当我使用shared_ptr和堆栈溢出以这种方式初始化时,它需要超过2 GB.
std::vector< std::shared_ptr<voxel> > vi(n);
for (size_t i = 0; i < n; i++)
{
vi.push_back(std::shared_ptr<voxel>(new voxel()));
}
Run Code Online (Sandbox Code Playgroud)
这种行为可能是什么原因,我该如何避免呢?
补充说明: …