   [C++] How do I allocate a multidimensional array with new?
  Let's say I want to allocate:hash_set<Object *> Collisionmatrix [Worldwidth][Worldheight] ;If I do something likehash_set<Object *> * Collisionmatrix = new hash_set<Object *> [Worldwidth * Worldheight] ;then I cannot useCollisionmatrix [j]Because the compiler does not know the size of each line.
  If the width and height of the world are not constant, have you considered using boost::multi_array (or similar) to hold the hash sets?Quote:then I cannot useCollisionmatrix [j]Because the compiler does not know the size of each line.You can still index it in a random manner, using (i * width + j) to compute the linear co-ordinates from the multi-dimensional ones. This is what boost::mutli_array does internally.   ;  hash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;   ;#include <boost/multi_array.hpp>typedef boost::multi_array<hash_set<Object *>,2> hashSetArray;hashSetArray Collisionmatrix(boost::extents[Worldwidth][Worldheight]);;Quote:Original post by Dizzy_exehash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;Thanks. I think that will do the trick.   ;  It won't work if Worldheight is not a compile-time constant (some compilers might have an extension that allow this, but it won't be portable). If the width and height is constant, you should just use a fixed array like in your original post.Whatever you choose to do, you should certainly be using an RAII wrapper to alleviate the headache of worrying about memory management.   ;Quote:Original post by rip-offIt won't work if Worldheight is not a compile-time constant (some compilers might have an extension that allow this, but it won't be portable). If the width and height is constant, you should just use a fixed array like in your original post.Whatever you choose to do, you should certainly be using an RAII wrapper to alleviate the headache of worrying about memory management.You are right, Worldheight is variable.I have created my own array class (I don't have boost installed):template <class T>class Dynamic2darray{const int Numofcolumns ;  T * Tp ;public:  Dynamic2darray ( int Localnumofrows, int Localnumofcolumns ) :Numofcolumns ( Localnumofcolumns ),Tp ( ( Localnumofrows > 0 && Numofcolumns > 0) ?new T [Localnumofrows * Numofcolumns] : NULL ){}  ~Dynamic2darray()  {delete [] Tp ;  }  inline T * operator [] ( int i ){return Tp + ( Numofcolumns * i ) ;}  } ;;Quote:Original post by Dizzy_exehash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;Dear god...   ;  You could also do:vector<vector<hash_set<Object*> > > Collisionmatrix;   ;Quote:Original post by hiigaraQuote:Original post by Dizzy_exehash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;Thanks. I think that will do the trick.The aforementioned compilation issues aside, please don't do it that way :| Your 'Dynamic2darray' class is a much better solution.Quote:(I don't have boost installed):Well, you could install it - it's allowed :)I know Boost is a big download, but multi_array is header-only, so once you've got it downloaded, all you have to do is include the multi_array header and you're good to go.Quote:Original post by ZipsterYou could also do:vector<vector<hash_set<Object*> > > Collisionmatrix;IMO, that seems like a step backward, even from the OP's ad hoc multidimensional array class. (I don't see any particular reason to use a jagged array for this if it can be avoided.)   