C++ vector.push()和vector.reserve()上的错误分配
我正在尝试构建一个大小为C++ vector.push()和vector.reserve()上的错误分配,c++,vector,bad-alloc,C++,Vector,Bad Alloc,我正在尝试构建一个大小为772538368的GLfloat向量。在执行push_back()时,我收到bad_alloc错误 检查之后,我尝试reserve()为向量保留内存。但是,现在我在尝试reserve()本身时遇到了相同的错误 在我的机器上,vector.max_size:1073741823,它比我需要的要大。在其他细节中,我正在Windows 10上使用VS 2015。另外,请在下面找到相关的代码片段 我应该如何解决这个问题 相关代码段: int main() { vect
772538368
的GLfloat
向量。在执行push_back()
时,我收到bad_alloc
错误
检查之后,我尝试reserve()
为向量保留内存。但是,现在我在尝试reserve()
本身时遇到了相同的错误
在我的机器上,vector.max_size:1073741823
,它比我需要的要大。在其他细节中,我正在Windows 10上使用VS 2015。另外,请在下面找到相关的代码片段
我应该如何解决这个问题
相关代码段:
int main() {
vector<GLfloat> targetVector;
targetVector.reserve(772538368); //Attempt2 to reserve. Also tried resize()
vector<vector<vector<GLshort>>> my3DimensionalData;
//build my3DimensionalData //no problem here.
//targetVector.reserve(772538368); //Attempt1 to reserve.
for (GLint rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
{
for (GLint colIndex = 0; colIndex < numberOfCols; colIndex++)
{
for (GLint depthIndex = 0; depthIndex < numberOfDepths; depthIndex++)
{
//perform gymnastic here on my3DimensionalData and get data.
/*initially I was getting bad_alloc while pushing back
*elements in the following block.
*This led to Attempt1 and Attempt2 as shown above.
*/
targetVector.push_back(data1);
targetVector.push_back(data2);
...
targetVector.push_back(data7);
}
}
}
}
intmain(){
向量目标向量;
targetVector.reserve(772538368);//尝试2保留。还尝试了resize()
向量My3维数据;
//构建my3DimensionalData//这里没有问题。
//targetVector.reserve(772538368);//尝试1保留。
对于(闪烁rowIndex=0;rowIndex
您很可能需要64位版本。您需要超过3 GB的连续内存,这几乎是所有4GB内存空间。什么是sizeof(GLfloat)
?您需要sizeof(GLfloat)
*736.75MB内存进行处理。您的程序是否编译为32位?如果是这样,请更改为64位,否则您将无法在32位模式下创建如此大的数组。关于“在运行时,容器的大小可能会被限制为小于可用RAM数量的max_size()”的值。”虽然这可能很明显,但我不得不提到,在64位操作系统上运行是不够的。您还需要编译64位(正如这个答案所示)。