Jump to content
  • Advertisement

Archived

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

origil

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

This topic is 6190 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
Advertisement
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
UGGGH I HATE double pointers!!! I find it just a hell of a lot easier to make a single array and reference through as 2d array.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!