mreza kardgar
mreza kardgar

Reputation: 25

Max size in array 2-dimentional in C++

I want to execute large computational program in 3 and 2 dimension with size of array[40000][40000] or more ,this code can explain my problem a bit,I comment vector because it have same problem when I run it it goes to lib of vector, how to increase memory of compiler or delete(clean) some part of it when program running?

#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
int main(){
    float array[40000][40000];
    //vector< vector<double> > array(1000,1000);    
    cout<<"bingo"<<endl;
    return 0;
}

Upvotes: 0

Views: 70

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283684

A slightly better option than vector (and far better than vector-of-vector1), which like vector, uses dynamic allocation for the contents (and therefore doesn't overflow the stack), but doesn't invite resizing:

std::unique_ptr<float[][40000]> array{ new float[40000][40000] };

Conveniently, float[40000][40000] still appears, making it fairly obvious what is going on here even to a programmer unfamiliar with incomplete array types.


1 vector<vector<T> > is very bad, since it would have many different allocations, which all have to be separately initialized, and the resulting storage would be discontiguous. Slightly better is a combination of vector<T> with vector<T*>, with the latter storing pointers created one row apart into a single large buffer managed by the former.

Upvotes: 3

Related Questions