View Full Version : I would like to allocate a 2d array
starboarder2001
08-01-2003, 11:57 PM
I would like to allocate a 2d array.
-what i am doing now:
int data[MAX_X][MAX_Y];
-what i would like to do:
//i figured i could just do this...
int *data;
data = new int[100][100];
//but i was wrong of course
My question is...is there a way to do this? or do i have to do "data = new int[100*100];"
i am using this to store my terrain data. right now it only will load a preset bitmap size.
DrunkenCoder
08-02-2003, 01:17 AM
I really don't understand why you wan't to dynamicly allocate something that
have a static size that really sounds quite silly, but for solving the problem I would suggest having a look here (2D array in snippets) (http://www.devmaster.net/forums/index.php?showtopic=423) just be sure to read the discussion that follows.
starboarder2001
08-02-2003, 10:27 AM
I really don't understand why you wan't to dynamicly allocate something that
have a static size that really sounds quite silly, but for solving the problem I would suggest having a look here (2D array in snippets) (http://www.devmaster.net/forums/index.php?showtopic=423) just be sure to read the discussion that follows.
it is going to hold the array of vertexs for my terrain...my terrain isnt always going to be 128by128 like it is now :).
Ed Mack
08-02-2003, 12:22 PM
Call me simple, but I like doing this method (just seems nice and clear):
int w,h; // you fill these in somewhere along the line
int *data;
data = malloc(w * h * sizeof(int));
Fill it with two for loops (x and y), data[y * w + x] = someValue;
Then get the bits like so:
// What cell you want the data from
int x = 13;
int y = 4;
int cellValue = data[y*w + x];
later, free(data); too.. but this is just the method I like.. and you probably already know this, but hey it may help someone else.
Smokey97
08-02-2003, 11:56 PM
there IS DEFINATLEY a way to do this, and it's quite simple if you understand pointers.
basicly you have to do this:
int **data;
data = new *int[first array value];
for(int i(0); i < (first array value); j++) {
data[i] = new int[second array value];
}
very simple... all you had otdo was allocate the first pointer, then go thourhg the array of pointers and set them to arays...
there you have it, a nice dynamic multidimensional array :)
P.S. i know my coding style is evil, with the braces directly after the loop declaration.... you should see my if/else/if else statements :P
DrunkenCoder
08-03-2003, 03:28 AM
int **data;
data = new *int[first array value];
for(int i(0); i < (first array value); j++) {
data[i] = new int[second array value];
}
Still if it's always going to be 128x128 why allocate it dynamicly?
And I really hope you understand that this will be a cache killer...
Ed Mack
08-03-2003, 11:22 AM
You really are a bit drunk :)
3rd post:
it is going to hold the array of vertex for my terrain...my terrain isnt always going to be 128by128 like it is now smile.gif.
davepermen
08-03-2003, 12:43 PM
the simplest and in my opinion best way is to allocate and access it as a one-dimensional array. thats the only way you can use it directly then for example to send it to opengl/dx..
the int** data is NOT a 2d array. its an array of pointers to several individual 1d arrays. in the memory, its not the same, and its not a good way to use it as 2d array at all..
oh, btw.. DAVE IS BACK :D
Smokey97
08-03-2003, 07:28 PM
the int** data is NOT a 2d array. its an array of pointers to several individual 1d arrays. in the memory, its not the same, and its not a good way to use it as 2d array at all..
he needs to manage his memory dynamicly, but i seem to have missed the part about it being for terrain, so yes a 1 dimnesional dynamic array is best, it may need a multiplication and an addition ot act as a multi dimensional array, but alteast you can pass it directly to your graphics api as a vertex buffer :)
i'm assuming you know how to make a 1d array act as a 2d array... (fairly self explanitory)
DrunkenCoder
08-03-2003, 10:50 PM
You really are a bit drunk :)
3rd post:
it is going to hold the array of vertex for my terrain...my terrain isnt always going to be 128by128 like it is now smile.gif.
Whoops.... read that one a bit too fast.
the simplest and in my opinion best way is to allocate and access it as a one-dimensional array. thats the only way you can use it directly then for example to send it to opengl/dx..
Using a lightweight (not like the one in snippets)
wrapper would also simplify this and give consitent behavoiur with standard
2D arrays.
the int** data is NOT a 2d array. its an array of pointers to several individual 1d arrays. in the memory, its not the same, and its not a good way to use it as 2d array at all..
It's the only way to express jagged arrays in C but that's about the only thing
it should be used for, as I said in a previous post that will most likely thrash the
cache and it will also quite probably use more memory due to alignment and
book-keeping issues than the more appropriate 1D -> 2D mapping.
davepermen
08-03-2003, 11:37 PM
drunkencoder, my code IS lightweight :D just "transposed"..
still, a 1d array IS the way to go:D
starboarder2001
08-07-2003, 01:10 PM
yep that is what i decided to do. It worked quite well.
Now I can load any size..this one is 2048by2048
http://www.vision-software.org/dcscreenshot2.JPG
donBerto
08-07-2003, 03:47 PM
that is quiet nice. good work.
:yes:
davepermen
08-07-2003, 10:54 PM
looks nice! good work
baldurk
08-08-2003, 11:06 AM
is it me or is that ship the ship from descent 2?
donBerto
08-08-2003, 04:42 PM
it is! IT IS! i love the intro to descent 2.
:yes:
baldurk
08-09-2003, 01:21 AM
I tried to play Descent 2 a few days ago, but it was an old game and I don't know my soundcard/IRQ/DMA like I used to, so I couldn't play it. And my videocard was in VESA, not VGA :(.
Shame, it was a good game.
davepermen
08-09-2003, 04:55 AM
hm.. played descent on my old 486, and forsaken on n64 (the best version of forsaken, much bether than pc or playstation)..
both where cool games imho
starboarder2001
08-31-2003, 10:58 PM
is it me or is that ship the ship from descent 2?
I use to play d1 2 and 3 alot so yes I modeled the ship after it :).
I realy didnt spend much time on it i just wanted something to fly around with :P.
I would probably still be playing d3 if the community wasn't so strange :blink:.
baldurk
09-01-2003, 10:38 AM
(I won't bother starting on the necromancy.)
I never played d3. That was after I was in that "stage". I did a lot of doom 1/2 editing. Both level editing and with dehacked.
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.