Vector of vector storage limitations - getting “Mi

2019-09-01 08:24发布

问题:

In C++ I am reading in several large gridded datasets for processing. Recently, one of these data sets broke my code before I even got to the processing.

The data set had 5765 cols and 5872 rows for a total of 33852080 cells. Well under the individual vector capacity limit for a double, right? Or not? I'm trying to figure that out currently.

The exception is thrown trying to push_back the inner 2496th vector into the original vectors.

Here's the code:

slopeGrid.reserve(rows);  
flowDirGrid.reserve(rows);     
flowAccumGrid.reserve(rows);  
tempGrid.reserve(rows);
inflowGrid.reserve(rows);
TWIgrid.reserve(rows);


for(int k=0; k<rows; k++)
{
    elevation.push_back(*new vector<double>(cols));
    ATB.push_back(*new vector<double>(cols));
    Area.push_back(*new vector<double>(cols));

    slopeGrid.push_back(*new vector<double>(cols));   
    flowDirGrid.push_back(*new vector<double>(cols));     
    flowAccumGrid.push_back(*new vector<double>(cols));   
    tempGrid.push_back(*new vector<double>(cols));
    inflowGrid.push_back(*new vector<double>(cols));
    TWIgrid.push_back(*new vector<double>(cols));

}

回答1:

So you have 34 millions objects, right? If enough of those objects are big enough I think reaching the maximum of 2 gigs per Windows 32 bit process wouldn't be that hard...



回答2:

What worked:

I still ran into memory exception when the having removed the dynamic allocation, and using this format:

elevation.resize(rows, std::vector<double>(cols, NODATA));

But then, based on Tive's answer I was able to locate and follow this:

https://msdn.microsoft.com/en-us/library/wz223b1z.aspx

And that fixed it and got it working.



标签: c++ vector