Archived

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

2D arrays and people who can't sleep! (HELP!)

This topic is 5976 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 seem to have a problem with a virtual global 2D array. I initialized it to 64x64. When I try to access map[63][0] in the function which initialized the array, nothing fails, but when I try to access map[63][0] in a different function, the program crashes and gives out 3 error messages, it usually gives out these errors when trying to access inexistant items in an array.I can access all other items in the array including map[0][63] so I'm not sure what the problem here is. Here is the initialization:
  
map = new UCHAR*[map_width];

		for(int i=0;i<map_width;i++)
			{
			map[i]= new UCHAR[map_height];
			}
		
		for(int i=0;i<map_width;i++)
			for(int j=0;j<map_height;j++)
				{
				map[i][j]=1;
				}
    
The map variable is declared as UCHAR **map; What am I doing wrong? Any help would be greatly appreciated. Edited by - origil on August 4, 2001 3:12:50 PM

Share this post


Link to post
Share on other sites
i think your intializing your array incorrectly, but your posted code looks screwed up to.

this might work
  

UCHAR* map = new UCHAR[map_width];



Edited by - omegasyphon on August 4, 2001 10:47:11 AM

Share this post


Link to post
Share on other sites
omegasyphon, i need it to be
int **map = new UCHAR*[64];
Otherwise the array is 1D,
I know the code i posted was messed up, I''ll edit it as soon as the server doesn''t give out an error.

Thanks for the quick reply though

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think a simpler solution is to use a vector of vectors:

vector> Map(map_height, vector(map_width));

you can access this just like a multidimensional array.

for(int i=0; i < map_height; i++)
{
for(int j=0; j < map_width; j++)
{
Map[j] = 1;
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

int* pArray = new int[ width * height ];

int nAtXY = pArray[ x + y * width ];


or


int** ppArray = new int*[ width ];
for( int i=0; i ppArray[ i ] = new int[ height ];

Share this post


Link to post
Share on other sites
I thank you all for the replies, but I am pretty sure my initialization code is okay since it''s not the first time i use it.
My only problem is that I can''t access any item from
map[63][0] to map[63][63] after I leave the initialization function.

Right after I initialize I can access all items in the map array,
but after i try to access it using a different function, the program crashes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your initialization code is fine. You''re not doing anything wrong in that section of the code. I can''t believe how many of the people who replied said you were doing something incorrectly or said they were afraid of 2d arrays. Anyway...

You said you were using an initialization function for the map. In the initialization function are you passing the map variable by value or reference? If you''re passing it by value that''s your problem. Here''s what I mean, but in code:

int init(UCHAR** _map, int width, int height) {
_map = new UCHAR*[width];
for (int i = 0; i < width; i++) _map = new UCHAR[height];
// etc
}

// somewhere in the function that calls init
UCHAR** map = 0;
init(map);
//

The value of map is still 0 after the call to init. Prior to the return in init _map does point to a valid 2d array but since it is not a reference parameter map just points to 0.

The easiest way around this is to make _map a reference parameter or you could make _map of type UCHAR***, pass &map to init and every reference to _map would be replaced by (*_map).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"The map variable is a global so I don''t pass it to the function."
What?
I thought that''s why the hell we have global variables, so
they could be passed to any function.

Share this post


Link to post
Share on other sites
I might have a bad programming style but if I create a function to make the code look cleaner, and not for reuseability with different input of variables why would I need to pass a variable when I can just have the function know it because it is global?

  
UCHAR **map; //Global


void PaintMap() //Access data

{
map[63][63]=1;
DrawTile(x,y,map[63][63]);
}



Maybe my approach is wrong, but if I do pass all global variables as parameters the function will have more than 8 parameters which will never be changed.

Thanks for the reply, still trying to solve this thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
None of the code you''ve posted so far has any problems in it. You''re going to need to post more of the code, specifically at what lines you recieve the error messages. Also if you could clarify what you mea by "...trying tto access inexistant items...". By items do you mean a game item or a value stored at a specific index in the array?

Share this post


Link to post
Share on other sites
If I create an array like this for example:
int test[6];
and then try to access test[7],
the program will crash and generate 3 errors.
The same errors I receive when trying to access
map[63][0].
I have no idea which parts of the code to post since none of it has anything to do with the map array.
I initialize in one function, then access it in a different one, when I reach the function which accesses the code, the map array seems to have a few problems.
All data from map[0][ALL] to map[1][ALL] is messed up,
and map[63][ALL] seems to be have been deleted.

Share this post


Link to post
Share on other sites
Finally solved it!
The initialization function was inside the winproc which caused a problem for some reason.

Instead I put the initialization function inside the main game loop.
I have no idea why it caused this problem because virtual variables have no scope unless deleted.

Anyways, thanks for all of the replies.

Share this post


Link to post
Share on other sites