Advertisement

Dynamic Allocation of Arrays

Started by June 03, 2000 03:04 AM
2 comments, last by wise_Guy 24 years, 6 months ago
Dear extremely handsome and smart and witty and everything else members of gamedev, I have been trying to solve my own problems like a good newby should, by looking for information here. I have run into a brick wall, and am not sure whether my compiler (borland C++ 5) is at fault or me. Here is my code, for a dynamically allocated tile map: int **map; map = new int[10][10]; if (map==NULL) cout << "error"; map[4][4] = 10; cout << map[4][4]; Okay by my understanding this should work... but it doesn''t. It compiles just fine but hangs, and the compiler tells me "Thread Stopped", reffering to the line: map[4][4] = 10; There is no output whatsoever. I am very confused because this works fine for dynamically allocated 1D arrays! Please don''t kill me for asking stupid questions or just being a stupid person! Thanks in advance, wise_guy
quote: Original post by wise_Guy

Dear extremely handsome and smart and witty and everything else members of gamedev,
ohhh, thanks, I feel so flattered now - Mad Keith
[snip]
Here is my code, for a dynamically allocated tile map:

int **map;
map = new int[10][10];

[snip]
There is no output whatsoever. I am very confused because this works fine for dynamically allocated 1D arrays!



Okay, I''m not SURE about this, but I believe this is not the way to dynamically allocate multidimensional arrays.

I think this would work ( My CPP book doesn''t mention dynamic allocation of multiple dimensions in a multiple dimension array ):

int **map;map = new (int*)[10];for( int i = 0; i < 10; i++ ){ map = new int[10];} 


You could of course get away with a single-dimension array...
int *map = new int[100];int map_x_y = map[10*y + x]; 


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
OK, the problem is that in C/C++ a two-demensional array isn''t an array of pointers to a 1D array. So
int **map;map = new int[10][10]; 

isn''t valid (you didn''t get a compiler error? VC gave me an error).
When I need a 2D array I use the second method descibed by MadKeithV.
Ahhh... it starts to make a little more sense now...

Thanks a heap MapKeithV!

btw VolkerG - 1D arrays are just the same I know, but 2D just work better with my brain!


wise_guy

This topic is closed to new replies.

Advertisement