vector of array

Started by
3 comments, last by TyphoidHippo 14 years ago
hi, im having trouble try to implement an array of array which has different dimension. so eg i have 4 arrays int a[5] int b[6] int c[2] int d[7] and i want to put this into an array. its not possible to do it in multimdimensional array since the array must have the same length. my idea is to put it into a vector of arary but when i try to run this program

vector<double*> arrayvec;
int main (int argc, char * const argv[]) {
	
        // yes one million items
	double a[1000000];
	arrayvec.push_back(a);
	//std::cout << arrayvec[0][6];
}


it gives me error not enough memory. but when i remove the push back command, i can even put double the amount. any idea why this is happen and how can i tackle it? thanks in advance
Advertisement
If you're using Visual C++, then try increasing the stack size.
It sounds like you want a jagged array. Why not use std::vector<std::vector<int> >?
Quote:Original post by rip-off
It sounds like you want a jagged array. Why not use std::vector<std::vector<int> >?


Hmmm... I think vector<vector<int> > runs the risk of moving around large amounts of memory if the outer vector needs to grow its space. I believe there are plans to make the STL have move semantics, but for the time being it might be safer to use deque<vector<int> > instead. Of course, it depends a lot on the usage, what is known at construction time, etc.

Perhaps I've missed something in the question, but how about just this:

double** array2D = new double*[500];

array2D[0] = new double[1000000];
array2D[1] = new double[2];
array2D[2] = new double[500];

It's not really a tightly-packed array, I guess, but rather an array of pointers to arrays, but an std::vector of arrays would, also, not necessarily be a contiguous array. Personally I'd use std::vector<std::vector<double>> if for no other reason than the handy size() member of all the randomly sized arrays... but if it's giving you problems, then by all means - get nasty with it

Edit: Like _fastcall said - I suspect you are getting the out-of-memory error because you are making that array of 1 million doubles on the stack, which may well simply be too much. This might work:
double* array0 = new double[1000000];
arrayVector.push_back(array0);

Of course, remember to delete[] arrayVector[0] before popping it


[Edited by - TyphoidHippo on March 18, 2010 6:26:01 PM]

This topic is closed to new replies.

Advertisement