Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fucntion variable by ref/val droping info

Status
Not open for further replies.

BPMan

Programmer
Jun 25, 2002
163
US
I compiling a very large system with fortran, ada, c, and C++. The different componets are being compilied and placed in archives then linked toghether. A problem I am seeing is that functions written in C and referenced externally by other C functions seem to drop data. Example Program:

void main()
{
myFunction();
}

Output is:
var1 = 0;
var2 = 0.000;

in File1.c:
void somefunction(int var1, float var2){
printf("var1 = %d\n",var1);
printf("var2 = %f\n",var2);
}

in Files2.c:
void myFunction(){
int var1 = 5;
float var2 = 5.5;
somefunction(var1, var2);
}

However if I change somecfunction to:
void somefunction(int *var1, float *var2)
and pass the variables in by reference the output is correct. Can anyone explain why this problem is happening and a fix so i don't have to change all the functions to pass by reference. Also this is not happening in all cases. Thanks.

 
What do the prototypes look like? Presumably they are
Code:
extern void somefunction (int,float);
extern void myFunction (void);
Reason I'm asking is that in C, you don't need prototypes: they can be
Code:
extern void somefunction();
extern void myFunction();
The problem with this is that on some compilers floats may get converted to doubles. If the parameters are passed backwards as they sometimes are, then they may get clobbered.

One possible fix is to only pass doubles and to only convert them to float for printing. Sounds just as bad as your fix for pointers but maybe there aren't as many floats being passed round.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top