Dynamic Allocation Of 2D Arrays

Started by
15 comments, last by PyroBoy 22 years, 11 months ago
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?
Advertisement
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.
bad!
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.
-Shane Parker
tryforfulluse:
hmmmm. Not really! :-)
Mind explaining a little further?



Edited by - PyroBoy on April 22, 2001 6:42:50 PM
MrShaneParker:
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...

You know, i could swear i''ve done it the way you''re trying to somehow. Too bad i don''t have the source code...
-Shane Parker
The 1D array method is better in most situations. For a dynamic 2D array you have to allocate an array of pointers, then use a for loop to allocate memory to each of those pointers.

"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!
http://druidgames.cjb.net/
Yeah, i would stick with the 1d array method myself... i know it works. :D
-Shane Parker
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 statementdelete [] 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.

So, just wrap in into a class, sort of like this:
    template <class Type> class Array2D {  Type *Data;  Type **Begins;  public:    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!
http://druidgames.cjb.net/

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

This topic is closed to new replies.

Advertisement