c++ question

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
Let's say I set a pointer to a 2d array like so:

double (*ptr)[divisions+1]=terrain; // terrain is a 2d array

how would i access the first value? i tried ptr[0] but it doesn't work

Don't ask me why I wanna do this.. ;)
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
try *ptr[0]

other parts of your code may be wrong, i didn't take the time to look that close


edit: is terrain dynamically allocated? ptr is to be a pointer, so you should be assigning it the address, not the value.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
Ok
this is my def of terrain:
double terrain[divisions+1][divisions+1];

and then ptr:
double (*ptr)[(divisions+1)

basically what i want to do is to be able to index terrain 1dimenstionally.... have ptr[0] correspond to the first value... etc
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Huh? You have a 2D array, so you have to specify 2 indeces.

i.e. ptr[0][0]
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
What is going in the cells?
I really could use that info. Perhaps a loop could update and sort the cells?
Do you have an initialization function for your array?

Edit: also if this is 2d, you need two values to call with it..
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
no, that would defeat the purpose of what i'm trying to do

here's my function for filling the array if you're interested:

void generateTerrain() {

double rough=roughness;

terrain[0][0]=random();
terrain[0][divisions]=random();
terrain[divisions][divisions]=random();
terrain[divisions][0]=random();

for (int i=0;i<iterations;++i) {
int q=(1<<i);
int r=(1<<(iterations-i));
int s=(r>>1);
for (int j=0;j<divisions;j+=r) {
for (int k=0;k<divisions;k+=r) {
diamond(j,k,r,roughness);
}
}
if (s>0) {
for (int j=0; j<=divisions;j+=s) {
for (int k=(j+s)%r;k<=divisions;k+=r) {
square (j-s,k-s,r,rough);
}
}
}
rough*=roughness;
}
}

it's the diamond square algorithm for generating fractals

but anyway, what i'm trying to do is be able to index the terrain 1 dimensionally...
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
yes, there are two problems

one, as others have noted, is that you only specified one dimension for ptr.

two, is that ptr is of type double pointer. which means you can't set it equal to a 2-d matrix. you can set it to the address of terrain, but not terrain itself.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
hmmm...

is there any way that i would be able to index terrain using just 1 dimension? like 0 corresponds to the first value.. (divisions+1)^2 - 1 would correspond to the last value... etc
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Why not just use a 1D array, but treat it as a logical 2D array?

i.e. Suppose you have a 10-element array, then elements 0-4 correspond to row0, while 5-9 represent row1.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
well yes... the problem lies in that i wrote the algorithm using a 2d array and my friend wrote the rendering function for a 1d array

i don't wanna make another temp variable and waste memory....

or would just making a new array be the most efficient way to do it?
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0


<< well yes... the problem lies in that i wrote the algorithm using a 2d array and my friend wrote the rendering function for a 1d array >>



well i guess you'll either have to rewrite one person's work, or you can make a function that converts the 2d into 1d (or vice versa).
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
To map the 1D array to 2D, just use the following method:

If given the row and column of the logical 2D array, then the index into the 1D array is:
one_d_index = number_of_rows * row + column;

For the opposite case:
row = (int)(one_d_index / number_of_rows);
col = one_d_index % number_of_rows;

 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
you can downcast the 2d array pointer to its 1d counterpart. This assumes arrays are continous in memory address space ( something you can't assume in Java for example ) .... anyway example code ...


int matrix2d[2][2] = { { 0, 1 } , { 2, 3 } };
int* matrix1d = (int*)matrix2d;
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< you can downcast the 2d array pointer to its 1d counterpart. This assumes arrays are continous in memory address space ( something you can't assume in Java for example ) >>



It is not guaranteed in C/C++ either.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
sorry to ask again but why would the following give me a stack error:

for (int r=0;r<size;r++) {
for (int s=0;s<size;s++) {
terrain1d[temp]=terrain[r];
temp++;
}
}
temp is initialized to zero
terrain is [1025][1025]
and terrain1d is [1025*1025]
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< sorry to ask again but why would the following give me a stack error:

for (int r=0;r<size;r++) {
for (int s=0;s<size;s++) {
terrain1d[temp]=terrain[r];
temp++;
}
}
temp is initialized to zero
terrain is [1025][1025]
and terrain1d is [1025*1025]
>>




A stack error? There are a lot of reasons this could happen. Are you allocating terrain1d inside a function? Are you using recursion anywhere? You should probably post the whole function code (or wherever the error is).
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
wel i know that the for loops that i posted are causing the error because i put those loops in another function, only called that function, and i still got the stack overflow
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
alright, here it is:

void drawFractal() {

double x,z,inc;
int size=divisions;
double terrain1d[(divisions+1)*(divisions+1)];

size++;
inc=2.f/size;
z=-1.f;
int temp=0;

for (int r=0;r<size;r++) {
for (int s=0;s<size;s++) {
terrain1d[temp]=terrain[r];
temp++;
}
}

for (int i=0;i<size;i++) {
x=-1.f;
for (int j=0;j<divisions;j++) {
draw(x,terrain1d[(i*size)+j],z,x+inc,terrain1d[(i*size+j+1)],z);
if (i<divisions)
draw(x,terrain1d[(i*size)+j],z,x,terrain1d[((i+1)*size+j)],z+inc);
x+=inc;
}
if (i<divisions)
draw(x,terrain1d[(i*size)+j],z,x,terrain1d[((i+1)*size+j)],z+inc);
z+=inc;
}
}
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Try to reduce the "divisions" variable -- essentially to reduce the size of the array. See if that gets rid of the overflow. If it does, then you're running out of stack space.

Also, you do not want to be allocating the divisions array on the stack. Either make it global, or dynamically allocate it (be sure to delete it when you're done).
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
sorry, when you paste into a message on at it messes up the formatting

why would having a large array create a stack overflow? is there any way to keep this size but correct the error?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< sorry, when you paste into a message on at it messes up the formatting

why would having a large array create a stack overflow? is there any way to keep this size but correct the error?
>>



First, tell me if the error goes away when you reduce the size (or allocate it globally).