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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Telling a function which global variable to use without passing it 1

Status
Not open for further replies.

WebDrake

Programmer
Sep 29, 2005
106
PL
Hi all,

I have a setup where several functions use certain variables, which for that reason I've designated as globals.

Originally these were some pointers, let's call them,

Code:
int *x; double *y; int *z;

Which were used by functions f1(), f2(), f3(), ...

The pointers are assigned to point to malloc'ed arrays of size N. However, it became necessary to have several different versions of these pointers so that the system described by x,y,z could be copied, modified, and the two or more systems run in parallel to see how they diverge, meaning I had to have more than one set of x,y,z pointers:

Code:
int *xA, *xB, *xC;
double *yA, *yB, *yC;
int *zA, *zB, *zC;

Of which, given any call to f1, f2, etc., the three that would be used would be either xA,yA,zA or xB,yB,zB, or xC,yC,zC.

The question is: is there any way to tell the functions f1(), f2(), ... which trio of pointers to use without actually passing the pointers [e.g f1(xA,yA,zA)], which is a waste of time since they're all globals?

One way would be to create a struct type containing x,y,z, and just pass a pointer to different structs, but I think that would slow down the processes inside of the functions.

Another way (overkill) would be to write out separate functions f1A(), f2A(), f3A(), f1B(), f2B(), f3B(), etc., but that seems like an unnecessary pain in the ass making it almost impossible to update the code effectively.

Is there a better way? :)

This is a serious number-crunching program so speed is of the essence---it's vital that any way of getting round the slow-down effect of passing lots of stuff not be offset by a slow-down elsewhere (which is why I rejected the struct option).

Any ideas?

Many thanks,

-- Joe
 
Use an array[3] of pointers to store the vars, and pass in a 0, 1, 2 to the function, to use A B or C.

Syntax is:
int ** x
doblue ** y
int ** z

make each of your functions take a #, 0-2, to get the index of the first dimension, and then use x[func_argument][n], and so on.

That's one copy of each function, and you're only passing in a short.
 
Thanks for the suggestion. Interestingly, after a profiling test, it doesn't appear to make any difference. I'm guessing that the compiler (Intel C/C++ compiler) is so clever that it can actually take either version and get machine code of equal speed from it. After all, functionally nothing is changing, it's just being notated differently.

What a pity there isn't a language with the conceptual elegance of something like Python that can translate those elegant expressions into something with the speed of C or FORTRAN!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top