bad_alloc on vector.push() and vector.reserve()

bad_alloc on vector.push() and vector.reserve()

本文关键字:vector and reserve alloc bad push on      更新时间:2023-10-16

我正在尝试构建大小为 772538368GLfloat 向量。在做push_back()时,我遇到了bad_alloc错误。

检查完这个问题后,我试图为向量reserve()内存。但是,现在我在尝试reserve()本身时遇到了同样的错误。

在我的机器上,vector.max_size: 1073741823 ,这比我需要的要大。在其他细节上,我在Windows 2015上使用VS 10。另外,请在下面找到相关的代码片段。

我应该怎么做才能解决这个问题?

相关代码片段:

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);
            }
        }
    }
}

你很可能需要一个64位的构建。您需要超过 3 GB 的连续内存,这几乎是 4GB 内存空间的全部。