I would like to allocate a 2d array
#1
Posted 02 August 2003 - 06:57 AM
-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.
0100
0010
0001
#2
Posted 02 August 2003 - 08:17 AM
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.
#3
Posted 02 August 2003 - 05:27 PM
DrunkenCoder said:
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.
0100
0010
0001
#4
Posted 02 August 2003 - 07:22 PM
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.
#5
Posted 03 August 2003 - 06:56 AM
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
#6
Posted 03 August 2003 - 10:28 AM
Smokey97 said:
data = new *int[first array value];
for(int i(0); i < (first array value); j++) {
data[i] = new int[second array value];
}
And I really hope you understand that this will be a cache killer...
#7
Posted 03 August 2003 - 06:22 PM
3rd post:
Quote
#8
Posted 03 August 2003 - 07:43 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..
oh, btw.. DAVE IS BACK :D
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#9
Posted 04 August 2003 - 02:28 AM
Quote
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)
#10
Posted 04 August 2003 - 05:50 AM
Ed Mack said:
3rd post:
Quote
Quote
wrapper would also simplify this and give consitent behavoiur with standard
2D arrays.
Quote
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.
#11
Posted 04 August 2003 - 06:37 AM
still, a 1d array IS the way to go:D
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#12
Posted 07 August 2003 - 08:10 PM
Now I can load any size..this one is 2048by2048
0100
0010
0001
#13
Posted 07 August 2003 - 10:47 PM
:yes:
#14
Posted 08 August 2003 - 05:54 AM
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#16
Posted 08 August 2003 - 11:42 PM
:yes:
#17
Posted 09 August 2003 - 08:21 AM
Shame, it was a good game.
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
#18
Posted 09 August 2003 - 11:55 AM
both where cool games imho
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
#19
Posted 01 September 2003 - 05:58 AM
baldurk said:
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:.
0100
0010
0001
#20
Posted 01 September 2003 - 05:38 PM
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.
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












