views:

114

answers:

5

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 *un*hooking 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.

A: 

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

Novemberland
+1  A: 

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.

Jay
A: 

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?

Pavel Radzivilovsky
Do you have a concrete example for an "inevitable" global variable?
Secure
+1  A: 

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.

anon
I would upvote this if not for the cavalier attitude in the first sentence. It may be worth it, but it is not easy; in fact it can make for a lot of work to thread this kind of global data throughout an already-large program, especially if a function deeply nested in the call tree requires access to the new variable.
pelotom
I meant to be "cavalier" - if it is difficult to do, the data almost certainly should not be global.
anon
+2  A: 

"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).
Brock Adams
It seems like static variables located in different files might be a lot harder to keep track of than a global variable, prefixed with something, in a single globals.h file.
guitar-