Allocation of a 2D Array ???

Started by
7 comments, last by thekid 22 years, 9 months ago
I need to have dynamic allocation of a 2d array but I cant figure out how. I have this declared in my class Example: class{ int numthingsX,numthingsY; thing *things; } when I initialize this class I want to take the values inputed for the size of the array and allocate that array so it will work like this things[x][y] I tried a few different methods but it doesn''t seem to work the same as a 1D array (Example: things[x] is done buy things = new thing[x]) Anyone help?
Advertisement
it''s my understanding that you can only create 1 dimetion dynamic arrays, so you might have to do a things[x*y] and loop through the array by useing the y value as an offset. i could be wrong, and i hope i am because doing that way gets verry tiresome. anyway, hope that helps.

-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
You allocate the 1D array as per usual

Things = new thing[X];

for(int i = 0; i++; i < Y)

Things[X] = new Things[Y]

get the idea?
quote:

I have this declared in my class
Example:

class{

int numthingsX,numthingsY;

thing *things;
}

when I initialize this class I want to take the values inputed for the size of the array and allocate that array so it will work like this

things[x][y]

I tried a few different methods but it doesn''t seem to work the same as a 1D array....



Correct, it doesn''t work the same way as a 1D array, so you need to do it differently. When you index into a 1D array, eg things[ 3 ], the program generates the pointer to things[ 3 ] by calculating ( things + 3 ).

When you declare a 2D array statically such as:

Thing SomeThing[ 10 ][ 20 ];

The compiler knows how wide the array is, so when you try to index into the array, ie access a particular element, to something with that element eg

  for( x=0 ; x< 10 ; x++ ){    for( y=0 ; y<20 ; y++ )    {       SomeThing[ x ][ y ].DoSomething();    }}  



The compiler knows to translate the SomeThing[ x ][ y ] into SomeThing[ x * 20 + y ] into ( SomeThing + x*20 + y ), to generate the correct pointer for the element you''re trying to access.

When you declare a dynamic 2d array, the compiler has _no idea_ how wide your array is going to be so it can''t use this method to calculate the correct pointer when you try to index into the array.

What you have to do is declare an array of pointers to pointers to create the array dynamically, which is not as scary as it sounds ;-).


class{

int numthingsX,numthingsY;

Thing **m_Thing;
}

Okay so m_Thing is a pointer to an array of pointers, so in the constructor do;

m_Thing = new *Thing[ numThingsX ]; //This creates an array of pointers.

for( x=0 ; x{
m_Thing[ x ] = new Thing[ numThingsY ];
}

//This creates numThingsX 1D arrays of ''Thing'' and stores the pointer to each 1D array in the correct entry in m_Thing[].

Your program can calculate the pointer correctly, such as

m_Thing[ x ][ y ]

pointer to array of pointers = m_Thing
pointer to array of things = m_Thing + x
pointer to thing (x, y ) = pointer to array of things + y

Hope that helps, (and makes sense).

cheers
Dan

ps That is how you new an array of pointers (new *Thing[ numThingsX ]) isn''t it ? I''m in an Internet cafe and can''t double-check....





Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
I don't know if you still need this but here it is.

int** p;

p = new int* [3] ;


for (int i = 0; i < 3; i++)
{
p = new int ;
}

p [0][0] = 2;
p [1][0] = 3;
p [2][0] = 4;

for ( i = 0; i < 3; i++)
{
delete [] p;<br> }<br> <br> delete []p;<br> <br> }<br><br>this works I tried it. </i> <br><br> All that is gold does not glitter,<br>Not all those who wander are lost;<br>The old that is strong does not wither,<br>Deep roots are not reached by frost.<br><br><br>Edited by - alchemar on July 23, 2001 8:36:09 PM<br><br>Edited by - alchemar on July 23, 2001 8:38:14 PM<br><br>Edited by - alchemar on July 23, 2001 8:39:53 PM
float **Array;

Array = new float*[x] ;

for (int n=0;n{

Array[n]=new float[y] ;

}

This makes an array, equivolent to:
float Array[x][y] ;

remember to:

for (int n=0;n{

delete [] Array[n] ;

}

delete [] Array;
delete Array;

to deallocate the memory.

The exact same method can be extended to any number of dimensions. Remember though, that this is not accessed via (necessarily) a consecutive block of memory. Because each of the y length blocks, are allocated independantly, and hence could be anywhere... So you cannot rely on [a*x+b] type addressing.

Edited by - dmounty on July 26, 2001 8:13:51 PM
I asked this question not so long ago - you don't have to mess around with indices and loops. You do it like this:

int (*PaulInt)[3] = new int[3][3];

Or to simplify:

int (*PaulInt)[3]; // declare a pointer to an array of 3 integers

PaulInt = new int[3][3]; // allocate an array of 3 pointers to 3 integers

Don't forget to delete them when you're done:

delete [] PaulInt;

Thanks Stoffel,
Paulcoz.

Edited by - paulcoz on July 26, 2001 5:27:19 AM
The method you just described, means for an n dimensional array, you need to know n-1 off the dimensions in advance... the methods described here, let you make you n dimensional array, and size/shape you want it to be.
Ah, I see - it''s a slightly different question than mine, sorry.

Paulcoz.

This topic is closed to new replies.

Advertisement