[C++] How do I allocate a multidimensional array with new?

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

Recommended Posts

Let's say I want to allocate:

hash_set<Object *> Collisionmatrix [Worldwidth][Worldheight] ;

If I do something like
hash_set<Object *> * Collisionmatrix = new hash_set<Object *> [Worldwidth * Worldheight] ;

then I cannot use
Collisionmatrix [j]
Because the compiler does not know the size of each line.

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

Share on other sites
hash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;

Share on other sites
#include <boost/multi_array.hpp>typedef boost::multi_array<hash_set<Object *>,2> hashSetArray;hashSetArray Collisionmatrix(boost::extents[Worldwidth][Worldheight]);

Share on other sites
Quote:
 Original post by Dizzy_exehash_set

Thanks. I think that will do the trick.

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

Share on other sites
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 ) ;	}  } ;

Share on other sites
Quote:
 Original post by Dizzy_exehash_set

Dear god...

Share on other sites
You could also do:

vector<vector<hash_set<Object*> > > Collisionmatrix;

Share on other sites
Quote:
Original post by hiigara
Quote:
 Original post by Dizzy_exehash_set

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 :)

Quote:
 Original post by ZipsterYou could also do:vector > > 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.)

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631420
• Total Posts
2999990
×