user2855778
user2855778

Reputation: 137

work with large size vectors in c++

I want to work with large size vectors. But these vectors allocated large size in memory and caused error..

size=10000;
//2D vector doubles 
vector<vector<double>> vecA(size,vector<double>(size));
vector<vector<double>> vecB(size,vector<double>(size));
vector<vector<double>> vecC(size,vector<double>(size));

I want to work with them in all of my code in program. what is best solution?

Upvotes: 2

Views: 3843

Answers (2)

deeiip
deeiip

Reputation: 3379

Firstly about your problem:

vector<vector<double> > v;
cout<< v.max_size();

This piece of code giving me the output 268435455 and 768614336404564650 (when compiled for 64-bit). Moreover in my machine it is not throwing any compilation error, but the programs hangs (i.e. the allocation never happens in 32-bit). Though in 64-bit all three vectors are being allocated with no error. So basically this may be a bug in vs12 or may not be a bug, Just a undefined behaviour (since the c++ standard does not garuntee anything about this allocation ).

Now about your solution: You may use a on-disk data structure, which will be much much slower. there are many library to do this for you. You may check HERE to find one.


Similar bug in vs.

Upvotes: 1

maninalift
maninalift

Reputation: 723

The tool you need depends on what you are trying to achieve. However, pre-allocated large vectors of pre-allocated large vectors is almost certainly not the right choice.

If the vector size remains fixed you may be creating a matrix-like thing, in which case you are better off using a matrix library such as the excellent Eigen.

If you are doing matrix calculations with large matrices, it is worth considering whether the performance would be better with sparse matrices (in other words is the data sparse).

If you are doing maths with large data arrays you should probably also consider using a GPU library because to can get speed ups of 10x to 100x. I believe Eigen can be made to use the GPU but I have never done so myself.

If you a are building a large table that is not going to be used like a matrix then you may need some other data-structure, perhaps something on-disk and database-like. Please post some more details of what you are trying to do.

Upvotes: 0

Related Questions