Skip to main content
GameDev.net gamedev.net
🔒 Locked

Emulated 2D Array

Started by Programmer One Apr 12, 2002 at 9:01 PM 9 replies 350+ views
Original Post
Programmer One
Programmer One
I want to allocate a 2D array dynamically, but that won't fly...so, I am dynamically allocating a normal 1D array, then, to get an emulated 2D array...I use this:
    
// Translate Grid

int cGrid::translateGrid( int X, int Y )
{
	// Pretend this is a 2D array

	int arrBuf = X % xSize;
        // xSize is emulated X side size of the '2D' grid.

	
        // Return the actual position from the 2D position

        return (arrBuf + Y);

}
  

To use this in my dynamically allocated array, i do:

      
theGrid[ translateGrid( xPos, yPos ) ];
    
Does ^ THAT ^ do the trick...? It seems to... _____________________________________________________ ICQ #: 149510932 Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info "Imagination is more important than knowledge." - Albert Einstein [edited by - Programmer One on April 12, 2002 10:03:16 PM]
Oluseyi
Oluseyi
Nope. You''ll never get past the first row. Maybe half-way into the second. You want this instead:

int compute_offset( int x, int y, int width, int height )
{
return ( (y * width) + x ) % ( (width * height) - 1 );
}


On a related note, why can''t you dynamically allocate a 2D array?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Programmer One
Programmer One
quote:
Original post by Oluseyi
Nope. You''ll never get past the first row. Maybe half-way into the second. You want this instead:

int compute_offset( int x, int y, int width, int height )
{
return ( (y * width) + x ) % ( (width * height) - 1 );
}




Thanks, Oluseyi.

quote:

On a related note, why can''t you dynamically allocate a 2D array?



It won''t work.
I do the following, and it is pointed out as an error:


something = new int [xSize][ySize];


Maybe I''m doing something wrong...

Also, the following line is also an error:

delete [][] something;


For the record..I have:

int *something;



_____________________________________________________

ICQ #: 149510932

Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info

"Imagination is more important than knowledge." - Albert Einstein

PaladinGLT
PaladinGLT
You gotta do something like
int **a;
*a = new int[maxX];
for (int i=0; i a = new int[maxY];

don''t know if thats exact.... i''ve done it once before.
PaladinGLT
Oluseyi
Oluseyi
PaladinGLT: Precisely.

Programmer One:

type **double_array = new type *[ dimension_1 ];
for( int n = 0; n < dimension_1; ++n )
double_array[ n ] = new type[ dimension_2 ];


When deleting the 2D array, apply in reverse:

for( int n = 0; n < dimension_1; ++n )
delete [] double_array[ n ];
delete [] double_array;


Alternatively, use std::vector:

std::vector< std::vector< type > > double_vector;

Then simply use resize to set the dimensions for each of the vectors. It''s overkill for a simple rectangular grid, but it''s much preferred for more complex solutions.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
krikkit
krikkit
How much overhead are you looking at if you want to use a 2-dimensional vector in order to accomplish a dynamic 2-d array?
ie, vector< vector > ?
Oluseyi
Oluseyi
quote:
Original post by krikkit
How much overhead are you looking at if you want to use a 2-dimensional vector in order to accomplish a dynamic 2-d array?
ie, vector< vector<int> > ?

Not much really. A vector is internally very similar to a straight, dynamically-allocated array. Indexing is simply a matter of pointer offsets, and the vector performs an internal reallocation when it needs more space in order to guarantee contiguous access.

If your internal vectors ("rows") do not change size dynamically (ie are fixed) for the entire duration of their use, there is virtually zero overhead post-initialization. If the rows only grow, then you might actually have a negative overhead (ie, using vector provides benefits both at design time - you don''t have to write the reallocation code - and all changes are atomized). If your rows grow and shrink, then you don''t even want a vector. You want a list.

So there''s no reason not to use vector... I should actually do more rigid testing but I''m very lazy.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
EvilCrap
EvilCrap
if your worried about speed then you want to use a vector (1D array), and treat it like a 2D array. however, speed will only be increased in sequential access (not random).

a person
a person
btw just an fyi:

a 2d array can be linear or planar. thus both of the following allocate a 2d array. no emulation needed, heh.


  
// 2d arrays, can be extended to ANY dimension array


int *linear = (int*)malloc(width*height*sizeof(int));

int **planar = (int**)malloc(height*sizeof(int*));
for(i=0; i<height; i++)
planar[i] = (int*)malloc(width*sizeof(int));

// access

linear[x+y*width];
planar[y][x];

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.