Jump to content
  • Advertisement
Sign in to follow this  
hiigara

[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.

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

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 this post


Link to post
Share on other sites
Advertisement
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 use
Collisionmatrix [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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Dizzy_exe
hash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;


Thanks. I think that will do the trick.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by rip-off
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by Dizzy_exe
hash_set<Object *> (*Collisionmatrix)[Worldheight] = (hash_set<Object *> (*)[Worldheight])new hash_set<Object *> [Worldwidth * Worldheight] ;


Dear god...

Share this post


Link to post
Share on other sites
Quote:
Original post by hiigara
Quote:
Original post by Dizzy_exe
hash_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 Zipster
You 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.)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!