Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - program structure (avoiding global variables, includes, etc.)

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.

like image 548
guitar- Avatar asked Jul 08 '10 22:07

guitar-


People also ask

Should you avoid global variables in C?

Global variables can be altered by any part of the code, making it difficult to remember or reason about every possible use. A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code.

How do you avoid global variables?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.

What can I use instead of global variables in C?

Use Local Variables Instead If your global variable is only ever accessed from one script, make it a "script local" instead.

How can parameters be used to avoid the use of global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.


2 Answers

"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).
like image 170
Brock Adams Avatar answered Sep 23 '22 01:09

Brock Adams


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.

like image 23
Jay Avatar answered Sep 22 '22 01:09

Jay