Jump to content
  • Advertisement


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


Dynamic Allocation Of 2D Arrays

This topic is 6299 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 dynamically allocate a 2D array of user-defined types, like so:

struct TILE2D
     BYTE type;
     float minTU;
     float minTV;
     float maxTU;
     float maxTV;

TILE2D *tileArray = NULL;
int tilesX = 30;
int tilesY = 30;

tileArray = new TILE2D[tilesY][tilesX];

The VC++6 compiler error I get is: error C2540: non-constant expression as array bound ...so I changed the last line to read:
tileArray = new TILE2D[30][30];
And I get this error: error C2440: ''='' : cannot convert from ''struct TILE2D (*)[30]'' to ''struct TILE2D *''. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast What gives? I''m looking at an example in a book that allocates arrays in this fashion using new, but the compiler won''t play along... Anyone done this before?

Share this post

Link to post
Share on other sites
i just quickly glanced at your code and it seems you are creating a new (Your Struct) [Array][];
i dont know how you could make a variable equal to a struct
so instead of doing tilearray=new TILE2D[][]
make a variable with the structure of the TILE2D array. i hope that made sense.

Share this post

Link to post
Share on other sites
Hey. I tried out a different variation of that code and got the same result. I know i''ve done it that way before... but that was using Turbo C or something, i can''t remember. Anyway, here''s a way you COULD do it...

tileArray = new TILE2D[tilesY*tilesX];

This would create an array of the right size. To access the elements of the array,you just do this:

blah = tileArray[yindex+1*xindex+1];

That should work all the same... The +1 is there just to make sure you don''t try to use a zero. :D

I hope this helps.

Share this post

Link to post
Share on other sites
hmmmm. Not really! :-)
Mind explaining a little further?

Edited by - PyroBoy on April 22, 2001 6:42:50 PM

Share this post

Link to post
Share on other sites
Yep, that''s certainly a way around it... But that''s just using a 1-d array to BS a 2-d one. There MUST be a way to dynamicly allocate a 2D array...

Share this post

Link to post
Share on other sites
My book(C How to Program, 2nd edition) states:

An array can be created and assigned to int * chessBoardPtr as follows:

chessBoardPtr = new int[8][8];

This array can be deleted with the statement

delete [] chessBoardPtr;

I thought it may be an issue with the user-defined types, so I switched my code around to just allocate some ints, and I get the same errors. I really don''t envy the thought of writing an index equasion every time I access the array. I''d like to just write
tileArray[y][x] or something like that, and have it access the right spot on the array.

Share this post

Link to post
Share on other sites
So, just wrap in into a class, sort of like this:
template <class Type> class Array2D {
Type *Data;
Type **Begins;

inline Type *operator [] (unsigned int a) {
return Begins[a];

Array2D(unsigned int px, unsigned int py) {
Data = new Type[px*py];
Begins = new Type *[py];
for(unsigned int a=0; a<py; a++) {
Begins[a] = &Data[px*a];
inline ~Array2D(void) {
delete [] Data;
delete [] Begins;

// Used like this:

Array2D <MyClass> MyArray(10,10);
MyClass ABC = MyArray[2][2];

Hopefully I didn't make any typos in there . You may want to add some error checking into that though.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP's Invasive Production Activation Technology!

Edited by - Null and Void on April 22, 2001 7:20:22 PM

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!