Jump to content
  • Advertisement
Sign in to follow this  

vector of array

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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];
	//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

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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];

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

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!