在我的程序中,我需要创建一个大型 3d 数组(~1GB),我当前使用两个 for 循环来初始化数组:
float*** array = new float** [m_width];
for (unsigned int i = 0; i < m_width; i++)
{
array3d[i] = new float * [m_height];
for (unsigned int j = 0; j < m_height; j++)
{
array3d[i][j] = new float[m_layers];
}
}
Run Code Online (Sandbox Code Playgroud)
该代码工作正常,但速度非常慢。所以我的问题是:是否有更快的方法来初始化/保留多维数组的内存?(几乎可以立即创建一维数组)
I have definded the following class witch creates and frees an opaque object (e.g. an operating system handle)
class A
{
public:
A(...)
{
allocateHandle(&h);
}
~A()
{
freeHandle(h);
}
SomeHandle h;
}
Run Code Online (Sandbox Code Playgroud)
When creating and resizing std::vector of A, the program crashes.
std::vector<A> vec;
vec.reserve(2);
vec.emplace_back(...);
vec.emplace_back(...);
vec.emplace_back(...); //crash
Run Code Online (Sandbox Code Playgroud)
When std::vector reallocates memory, it calls the move constructor on all the objects, thus also moving the handle in A (which is basically an integer).
However, it also calls the …