0
101 Aug 02, 2003 at 06:57

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.

#### 19 Replies

0
101 Aug 02, 2003 at 08:17

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) just be sure to read the discussion that follows.

0
101 Aug 02, 2003 at 17:27

@DrunkenCoder

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) 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 :).

0
101 Aug 02, 2003 at 19:22

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.

0
101 Aug 03, 2003 at 06:56

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 = 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

0
101 Aug 03, 2003 at 10:28

@Smokey97

int **data;
data = new *int[first array value];
for(int i(0); i < (first array value); j++) {
data*= 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…

0
101 Aug 03, 2003 at 18:22

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.

0
101 Aug 03, 2003 at 19:43

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

0
101 Aug 04, 2003 at 02:28

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)

0
101 Aug 04, 2003 at 05:50

@Ed Mack

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.

0
101 Aug 04, 2003 at 06:37

drunkencoder, my code IS lightweight :D just “transposed”..

still, a 1d array IS the way to go:D

0
101 Aug 07, 2003 at 20:10

yep that is what i decided to do. It worked quite well.

Now I can load any size..this one is 2048by2048
[img]http://www.vision-software.org/dcscreenshot2.JPG[/img]

0
101 Aug 07, 2003 at 22:47

that is quiet nice. good work.

:yes:

0
101 Aug 08, 2003 at 05:54

looks nice! good work

0
101 Aug 08, 2003 at 18:06

is it me or is that ship the ship from descent 2?

0
101 Aug 08, 2003 at 23:42

it is! IT IS! i love the intro to descent 2.

:yes:

0
101 Aug 09, 2003 at 08:21

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.

0
101 Aug 09, 2003 at 11:55

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

0
101 Sep 01, 2003 at 05:58

@baldurk

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:.

0
101 Sep 01, 2003 at 17:38

(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.