Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best strategy for sharing variables between source files in c/c++?

I frequently have to write c/c++ programs with 10+ source files where a handful of variables need to be shared between functions in all the files. I have read before that it is generally good practice to avoid using global variables with extern. However, if it is completely necessary to use global variables, this link provides a good strategy. Lately, I have been toying with the strategy of wrapping up all my variables in a struct or a class and passing this struct around to different functions. I was wondering which way people consider to be cleaner and if there are any better alternatives.

EDIT: I realize strategies may be different in the two languages. I am interested in strategies that apply to only one language or both.

like image 501
baconeer Avatar asked Aug 01 '13 15:08

baconeer


People also ask

Is it possible to use global variable in another .C file?

Every C file that wants to use a global variable declared in another file must either #include the appropriate header file or have its own declaration of the variable. Have the variable declared for real in one file only.

Why do we use extern in C?

“extern” keyword is used to extend the visibility of function or variable. By default the functions are visible throughout the program, there is no need to declare or define extern functions. It just increase the redundancy. Variables with “extern” keyword are only declared not defined.


2 Answers

Does it REALLY need to be global?

This is the first question you should always ask, is this variable used GLOBALLY e.g. in all contexts. The answer is almost certainly... no it's not.

Consider Context

Is the variable global state, or is it context? Global state is usually rare, context on the other hand is quite common. If it's global state consider wrapping in a singleton so you can manage the how of interaction with your globals. Using Atomic<> is probably not a bad idea, you should at least consider synchronization.

If it is context then it should be passed explicitly in a structure or class, as the data is explicitly relevant to that context an no-other. Passing context explicitly may seem like a burden but it makes it very clear where the context is coming from rather than just referencing random variables out of the ether.

What is the Scope?

It may seem odd to say that globals are scoped, but any global declared in a single file may be declared static and thus unlinkable from any other file. This means you can restrict who has access to the global state in a given scope. This allows you to prevent people from randomly tweaking variables.

like image 194
Mgetz Avatar answered Sep 17 '22 16:09

Mgetz


The better alternative to globals is to not use globals.

Don't try to sweep them under the rug using a struct or a namespace or a singleton or some other silly thing whose only purpose is to hide the fact that you're using globals.

Just don't ever create one. Ever.

It will force you to think of ownership and lifetime and dependencies and responsibility. You know, grown-up things.

And then, when you're comfortable writing global-free code, you can start violating all those rules.
Because that's what rules are for: to be followed, and to be broken.

like image 43
molbdnilo Avatar answered Sep 21 '22 16:09

molbdnilo