Question

I'm using C (not C++) and I'm unsure how to avoid using global variables.

I have a pretty decent grasp on C, its syntax, and how to write a basic application, but I'm not sure of the proper way to structure the program.

How do really big applications avoid the use of global variables? I'm pretty sure there will always need to be at least some, but for big games and other applications written in C, what is the best way to do it?

Is there any good, open-source software written strictly in C that I could look at? I can't think of any off the top of my head, most of them seem to be in C++.

Thanks.

Edit

Here's an example of where I would use a global variable in a simple API hooking application, which is just a DLL inside another process.

This application, specifically, hooks API functions used in another application. It does this by using WriteProcessMemory to overwrite the call to the original, and make it a call to my DLL instead.

However, when unhooking the API function, I have to write back the original memory/machine code.

So, I need to maintain a simple byte array for that machine code, one for each API function that is hooked, and there are a lot.

// Global variable to store original assembly code (6 bytes)
BYTE g_MessageBoxA[6];

// Hook the API function
HookAPIFunction ( "user32.dll", "MessageBoxA", MyNewFunction, g_MessageBoxA );

// Later on, unhook the function
UnHookAPIFunction ( "user32.dll", "MessageBoxA", g_MessageBoxA );

Sorry if that's confusing.

Was it helpful?

Solution

"How do really big applications avoid the use of global variables?"

  1. Use static variables. If a function needs to remember something between calls, use this versus global variables. Example:

    int running_total (int num) {
        static int sum  = 0;
        sum             += num;
        return sum;
    }
    
  2. Pass data via parameters, so that the value is defined one place, maybe main() and passed to where it is needed.

  3. If all else fails, go ahead and use a global but try and mitigate potential problems.

    1. At a minimum, use naming conventions to minimize potential conflicts. EG: Gbl_MyApp_DeveloperName. So all global variables would start with the Gbl_MyApp_ part -- where "MyApp" was something descriptive of your app.
    2. Try to group functions by purpose, so that everything that needs a given global is in the same file (within reason). Then globals can be defined and restricted to that file (beware the extern keyword).

OTHER TIPS

There are some valid uses for global variables. The schools started teaching that they were evil to keep programmers from being lazy and over using them. If you're sure that the data is really globally needed then use them. Given that you are concerned about doing a good job I don't think you'll go too far wrong using your own judgment.

It's easy - simply don't use them. create what would have ben the global variables in your main() function, and then pass them from there as parameters to the functions that need them.

The best-recomended open-source software to learn C in my college had been Linux so you can get examples from their source.

I think the same, globals are bad, reduces readability and increases error possibility and other problems.

I'll explain my example. I have a main() doing really few things. The most of the action comes from timers and external interrupts. These are functions with no parameter, due the platform I'm using, 32 bits micro controller. I can't pass parameters and in addition, these interrupts and timers are asynchronous. The easiest way is a global, imagine, interrupts filling a buffer, this buffer is parsed inside a timer, and the information parsed is used, stored, sent in other timers. Ok, I can pull up an interrupt flag and process in the main function, but when executing critical software that way is not good idea.

This is the only kind of global variable that doesn't make me feel bad ;-)

Global variables are almost inevitable. However, they are often overused. They are not a substitute for passing proper parameters and designing the right data structures.

Every time you want a global variable, think: what if I need one more like this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top