Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the ambiera forums
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:
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.
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.
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.
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!
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.
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.