Dynamic 2D array allocation

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Dynamic 2D array allocation

Post by grumpymonkey »

I'm workin on a class which uses 2D arrays for storing information, but I need to be able to change the size of the arrays during runtime. for example:
this is my array of size 0:

Code: Select all

numbers[0][0];
then lets say I wanted to add 1 slot to index 0, and 1 slot at index 1:

Code: Select all

numbers[0][1]=5;
numbers[1][1]=10;
how would I be able to do that? I tried creating the arrays before like this:

Code: Select all

int numbers[10][10];
which gives me a maximum of 10x10, but idk if I will have to add more, and not to mention the risk of stack overflow :(
Image
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Oy, std::vector or any similar vector class. Else, do the same, make your class, allocate and deallocate memory all the time while copying stuff around and hope to do it as efficiently as a vector.
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

well how would I use an std vector, or allocate/deallocate the array?
Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

If you really had to do it, you'd do something like this...

Code: Select all

std::vector< std::vector<int> > v;

v.resize(10);
for (size_t i = 0; i < 10; ++i)
  v [i].resize (10);
But I wouldn't do it. I suggest you read this and this and then reconsider.

Travis
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

oh I already solved this after like the first post. I used an array of arrays

Code: Select all

array<array<std::string>> Strings;
Image
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

One thing you need to note. Using an std::vector (or equivalent like irrlicht's array) for 2d dynamic arrays is a mistake. A vector cannot handle its contents resizing themselves. You need to use an std::deque or pass pointers to the second dimension of arrays to the first level.

For example:

Code: Select all

std::deque< std::vector<int> > my2Darray; //Works
std::vector< std::vector<int> > my2Darray; //Will fail if the 2nd dimension is resized
std::deque< std::deque< int > > my2Darray; //Also works
std::vector< std::vector< int >* > my2Darray; //Should work as well 
I learned this lesson the hard way, the bugs this causes have symptoms that are not very obvious.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

tbh all that reallocation is probably a really bad idea. could you not just allocate a 2d array of sufficient size at the start and then use as much of it as you need at each stage? You shouldn't really need to be resizing it I wouldn't think...

and obviously don't create a 2d array of like 1024x1024 or something just because you're not sure how big it might need to be. put realistic limitations on your application as memory don't come fo' free!
Image Image Image
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

JP is correct. What are you exactly doing anyways?

If I were to do this, why not just dynamically allocate a 1D array (one allocation) and use it as a 2D array by operator overloading? Rather than doing 1024 allocations of such small sizes. That will really fragment your heap.
TheQuestion = 2B || !2B
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

allocating the array before will take up a lot of memory and will cause a stack overflow error

@Dark_Kilauea
interesting, I never knew about an std::deque, I'll look it up.

btw, im using it like this:

Code: Select all

array<array<int>*> data
Image
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

grumpymonkey wrote:allocating the array before will take up a lot of memory and will cause a stack overflow error
Well if definitely shouldn't be overflowing the stack since you are allocating on the heap...
TheQuestion = 2B || !2B
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

You can not only redefine a vector contained in another vector, if you do it "right", it should be transparent to the user. The head of a std vector is normally only a pointer. That is what is contained in the first vector. If you resize from there, it will work fine. Just don't carry pointers around when you resize memory, it's always a dangerous game.
Post Reply