Archived

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

Accessing multidiemsional arrays passed in as pointers

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

Hey people, I''ve tried to search everywhere for a solution to this problem and i haven''t been able to find anything so i''m hoping one of you geniuses will be able to help me out I have this function: CreateGraph(int** map, int sizex, int sizey) map is a two dimensional array, the method gets called more than once with arrays of different sizex. Thats the reason why i''ve had to pass it in as int**. The problem now is that i can''t just access the array using the conventional map[0][0]. I''ve tried lots of ways to try and access the array, including just desperate guess work when all else was failing. Does anyone know how to solve this? Many thanks T

Share this post


Link to post
Share on other sites
If x and y are your coordinates:

int val_at_x_y = (*map)[x + xsize * y];

Make sure 0 <= x + xsize * y < (xsize * ysize) though.



Share this post


Link to post
Share on other sites
thanks for the quick reply

but i''m not sure how that code works?

If the array is 2D...surely x + xsize * y is if i just had one big 1D array acting as if it was a 2D array?

Share this post


Link to post
Share on other sites

The 2D array is converted to a 1D array by accessing the first element map[0]. In this case, it''s been dereferenced (*map), which is the same thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,

I used a structure similar to the following:


class Array
{
public:
struct elem{
T* data;
};

elem *data;

Array( int sizeX, int sizeY ){
data = new elem[sizeX];
for( int i = 0; i < sizeX; i++ )
data.data = new T[ sizeY ];
}

inline T GetData( int i, int j ){
return data[i].data[j];
}

/// etc ...

};

Share this post


Link to post
Share on other sites
hmm...i still seem to be having trouble.

i tried using parla's suggestion but i kept on getting an access violation. Onemind's suggestion was greeted with something similiar although int *mapLine = map[x] worked it was the second line which was causing problems.

when i ran in debug mode ( i wanted to check that i was actually passing in something and that it wasn't just null )

map is said to have value 0x004f8020 (good, not null and pointer address) int (* map_cost[24]) (map_cost is the array being passed in, size [40][24] )

after int *mapLine = map[x] though mapLine has value 0x0000000a, which is null i think.

That might mean i'm passing it in incorrectly

but surely if
int map_cost[y][x] = *whatever*, then to pass it in all i need to do is (int**)map_cost?

thanks again

[edited by - LifeOfTamir on June 8, 2004 12:29:55 PM]

Share this post


Link to post
Share on other sites
Arrays are zero-based.

If the array is 40x24, and you try to access element 24 in the last row, you''re out of bounds. (40x24 = 960 -- element 40,24 = 1025).

Share this post


Link to post
Share on other sites
I know arrays are zero based, this is when it''s trying to access the first element..i.e [0][0]

functino code:

CGraph* CGraph::CreateGraph(int** map, int sizex, int sizey, int tilesize)
{
CGraph* nodeSet = NULL ;

int ix, iy; //used for looping


int id = 1;

int countx = 0;
int county = 0;

int mapx = 0;
int mapy = 0;

int val_at_x_y;
int *mapLine;


//int halfTileSize = tilesize / 2;


int ypos = 0;//halfTileSize;

int xpos = 0;//halfTileSize;


for (iy = 0; iy < sizey; iy++)
{
for (ix = 0; ix < sizex; ix++)
{
mapLine = map[iy];
error is here ---> val_at_x_y = mapLine[ix];
//map[mapx][mapy] = cost; count = id

nodeSet = new CGraph(new CNode(NULL,new CNodeData(xpos,ypos,val_at_x_y),id),nodeSet);
id++;
mapx++;
countx++;

xpos = (countx*tilesize);//+halfTileSize;

}
mapy++;
mapx = 0;
countx=0;
county++;

xpos = 0;//halfTileSize;

ypos = (county*tilesize);//+halfTileSize;

}
.
.
.


this is the call
CreateGraph((int**)map_cost, WORLD_SIZEX, WORLD_SIZEY, TILE_SIZE)

Share this post


Link to post
Share on other sites
i believe you should be passing the array as an int * not as an int **. X-D arrays are really just 1D arrays stored in memory in the serial format: row1, row2, row3, etc... so by passing the array variable you get all the information you need (i.e. the head of the array).

you are making the assumption in your code that the array is actually stored 2 dimensionally in memory. it isn't. like i said, it's just a 1D block of memory: row1, row2, etc... go with the code that you got in your first reply.


this should work, but you might have to debug it.

int foo[10][10];
yourFunction( foo, 10, 10 );

yourFunction ( int *theArray, int width, int height )
{
for (int x = 0; x < width; ++x )
{
for (int y = 0; y < height; ++y )
{
int thisElement = theArray[width * y + x];
}
}
}


-me

[edited by - Palidine on June 8, 2004 12:52:56 PM]

Share this post


Link to post
Share on other sites
Brilliant! thanks alot guys!

okay the problem is solved but just so i know whats going on:

if 2D arrays are stored in serial format as Palidine said then does the end of row1 contain a pointer to the beginning of row2? No, the first array is an array of pointers pointing to arrays? I think i get wants going on but i can''t picture it...or write in words....maybe i don''t know whats going on *thinks*...

Share this post


Link to post
Share on other sites
no, there are no pointers anywhere in the memory space. it''s just a straight up block of memory. because the compiler knows that you declared the array as int foo[10][10], it''s basically doing that same math to access the [x][y] spot in the array. what this means is that you cannot create multi-dimensional arrays with new/malloc and access them with [][] operators. any multi-dimensional array you want to have on the heap will have to be accessed by the [width * y + x] method. if it''s 3-D, 4-D, etc, you can work out the math for accessing the array.

multi-dimensional arrays, are, for all intents and purposes a compiler trick.

-me

Share this post


Link to post
Share on other sites
quote:

what this means is that you cannot create multi-dimensional arrays with new/malloc and access them with [][] operators



You can create multidimensional arrays with new, and you can access them using [][] syntax.


int (*pint)[10] = new int[100][10];
pint[9][9] = 100;


[edited by - Jingo on June 8, 2004 2:09:04 PM]

Share this post


Link to post
Share on other sites