PDA

View Full Version : arrays



andy-m
04-09-2002, 02:49 AM
Hi guys, more of a c++ question. created some maths functions in a .h file to give me an array[10[2] that i want to pass back to a .cpp file to use to create a graph using a Bezier curve, however having problems passing or returning the array. any ideas???

Obliviator
04-09-2002, 03:51 AM
Originally posted by andy-m:
Hi guys, more of a c++ question. created some maths functions in a .h file to give me an array[10[2] that i want to pass back to a .cpp file to use to create a graph using a Bezier curve, however having problems passing or returning the array. any ideas???

what exactly is your problem?
could you show the problematic piece of code?

Deiussum
04-09-2002, 04:44 AM
More detail would be nice. Without more information about the only thing I could possibly see wrong is that if you are implementing your functions in a .h, you will have problems in the linking stage if you #include that .h in multiple files. (Those functions would attempt to get linked in twice, and you'd get a duplicate symbol error, or something to that effect.)

So far as passing arrays. Arrays are always passed by pointer. Which makes me think of another potential problem...

Example



float* myFunction()
{
float blah[1024]; // this is created on the stack

// do stuff with it
return blah;
} // function exits, memory for blah is cleaned off the stack...
// but you have a pointer to it...
// you try and use that pointer, but it points to memory that is now unallocated
// crash!!


A better way to handle something like that would be like so...




void myFunction(float* blah)
{
// manipluate blah when the function exits
// it will still be there because the memory
// is allocated outside of this function.
}


EDIT: Added code tags to 2nd block of code.

[This message has been edited by Deiussum (edited 04-09-2002).]

andy-m
04-09-2002, 05:19 AM
Hi guys, thanks, with the code below i can pass numbers in the 2d arry stored in the .h file to a 1d array in the .cpp (just 1 main file) but i cannot figure out how to get two accross yet??

got following variables and code in .h

double Pos[10][2];
const int q = 10;
int w = 0;

double passmeanarray(double X[q])

{
X[0] = Pos[w][0];
w++;
return X[0];
}

/// and following in .cpp

const int NumPar = 10;
void displayX2(double[NumPar]);
double cord[10];

void displayX2(double* arr)
{
for(int col = 0; col < NumPar; col++)
{
cout << ", " << passmeanarray(arr);
//cord[1] = passmeanarray(arr);
cout << " ! " << cord[0] << endl;
//cout << " n" << cord[0][1] << endl;
}
}

Gavin
04-09-2002, 05:29 AM
You may want to look at this code.....
http://www.cs.cf.ac.uk/User/G.R.Powell/applets/CODE/dyn_matrix.c

Deiussum
04-09-2002, 09:03 AM
Andy, I'm not quite sure what you mean by getting "two across." Do you mean you want to return two values?

Also if you just want to access the array you are putting in the .h file, you can do so directly if you want since it is just a global variable. The way #include works is basically the same as if you took all that code from the #include and pasted it into the .cpp file. The compiler doesn't really see them as 2 separate files.

andy-m
04-10-2002, 03:53 AM
Thanks guys for all your help, done it now had to turn the 2d array[10][2] into a 1d array[20] , pass it to the cpp file and then convert it back into a 2d array which i can now use to make my curves on my graph. There must be away of passing 2d arrays but i can't find it???? here the code if anyones intrested : -

in .h

double Pos[10][2]; //2D Array Used To Store Graph Plots
const int q = 1; //Int Used In passingmearray Function
int w = 0; // ""
int p = 0; // ""

double passmeanarray(double X[q])

{
if ( p == 0 )
{
X[0] = Pos[w][0];
p++;

return X[0];
}

if (p == 1)
{
X[0] = Pos[w][1];
p = 0;
w++;

return X[0];
}

else
{
cout << "INTERNAL MEMORY ERROR" << endl;

return 0;
}
}


in .cpp

//Variables Used In Passing Array Over In mathsys
const int NumPar = 20; // Used In displayX2

void displayX2(double[NumPar]); //Function Declaration

double cord[20]; //Double Array Used For Converting 1D Array To 2D Array
double Finalcord[10][2]; //2D Double Array Used For Final Graph Plots
int r = 0; //Int Used In displayX2
int t = 0; // "" "" "" ""


main()
{
displayX2(cord);
}

void displayX2(double* arr) //Reads 1D Array From maths.h And Saves Into 2D Array
{

for(int col = 0; col < NumPar; col++) //Loop Though 1D Array
{
passmeanarray(arr); //Pass Values Over

if ( r == 0 )
{
Finalcord[t][0] = cord[0]; //Save Into 2D Array
r++;
}

else if (r == 1)
{

Finalcord[t][1] = cord[0];
r = 0;
t++;
}

else
{
cout << "INTERNAL MEMORY ERROR" << endl; // Error Handler
}


}

/*for (int i=0; i<10; i++)
{

cout << " " << Finalcord[i][0];
cout << ", " << Finalcord[i][1] << endl;
}
*/
}

Deiussum
04-10-2002, 04:29 AM
Ahhh... I think I understand your problem, maybe. This might help. A 2d array is basically the same as a pointer to a pointer.

So in your case you could do something like so.

double passArray(double** array)
{
}

BTW, arrays are ALWAYS passed by pointer even if you try and do this.

double passArray(double array[10])
{
}

In the above example, the 10 is mostly meaningless. You could pass in an array that is size 2 or a size of 100. The compiler won't care. If you try and go past the bounds of the actual size of the array passed in, you'll be flirting with a crash. http://www.opengl.org/discussion_boards/ubb/smile.gif (i.e. if you pass in an array of size 2 and try and access element 3 there will be a problem even though you specified 10)

Hope that helps

Gavin
04-11-2002, 07:55 AM
Have a look at the link which I posted above!!