Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

pyroGX

How can I create a multi-dimensional array with new?

This topic is 5804 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

I''m trying to create a two-dimensional array of a struct that im using to represent a system of tiles inside of a class. So I figured I would have a pointer to the struct set up that will be allocated to the width and height of the map when it is loaded from disk. Then I read that in order to allocate a multidimensional array with new it has to look something like this: //a 20x20 array of myStruct myStruct (*s)[20]; s = new myStruct[20][20]; How would you create one if you didn''t know what the 2nd dimension would be before-hand? Or would I have to create it as a single dimensional array and just traverse it like myStruct[ x + y * rowLength ] in order to pull up an x-y coordinate? Anyone else have a similar problem and would like to share some insight? Thanks =)

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by pyroGX
Anyone else have a similar problem and would like to share some insight?

No, nobody''s ever had a C++ multidimensional array problem before. Ever.

Share this post


Link to post
Share on other sites
Ah thanks, sorry if I asked something that gets answered every day.

Will 'delete [] s;' work on that array or do you have to loop through and call delete [] on each part?

I have it set up similar to this now and was wondering because I havent had too much experience with pointers to pointers.


myStruct ** s;

*s = new myStruct[ x ];
for ( int index = 0; index < x; index++ )
s[ index ] = new myStruct[ y ];


[edited by - pyroGX on August 28, 2002 2:54:47 PM]

Share this post


Link to post
Share on other sites

// declaration
myStruct ** s;
s = new myStruct *[ 20 ];
for( int x = 0; x < 20; ++x )
s[ x ] = myStruct[ 20 ];
 
// deallocation
for( int x = 0; x < 20; ++x )
delete [] s[ x ];
delete [] s;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!