[LCP]global variable alternative

Joachim Bauernberger bj at gmx.net
Thu Apr 4 01:44:05 UTC 2002


Hi,

I just wrote a fairly large project with a huge amount of options to 
control it's behaviour. Further there are also the switches of external 
second party shared libs that can be linked in. Those libs add even 
more options to control their own functionality. Also I realize that 
there might even be more in future ...
  
Most of the options are typically switched on/off by querying a 
database for the users desired settings.

Many of these options are global variables since I reference them often 
within deep lying functions.

I made that choice because I thought setting up function stackframes 
with many parameters would be more overhead than just calling the 
function with less parameters, no?
Also when initially designing my goals of what should be achieved, I 
didn't even have half as much options/features and decided I could live 
with a few global variables.

Now that the amount of options increased dramatically I wonder if I 
would be able to save more resources and make my code more readable by 
declaring those global variables within main() by doing the following:

typedefine a structure in options.h that contains all the options that 
are global till now:
typedef struct opt_t {
	short option1;
	short option2;
	char *opt1;
	....
} opt_t;

options.h also includes a global definition of

opt_t *myoptions;

The main function then just needs to allocate space for the extra 
struct opt_t and initialize it's values.
The global myoptions pointer is then set to the local definition of 
main's opt_t.
This way I don't need to pass any values down to deeply nested 
functions nor are my variables global anymore.
It would also be much cleaner when reading the code since global 
variables don't appear from nowhere anymore.
 
Is this the correct approach or how would you solve this?

Thanks,
Joachim

--
http://www.mXscan.org/




More information about the linuxCprogramming mailing list