简体   繁体   English

C - 程序结构(避免全局变量,包括等)

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

I'm using C (not C++) and I'm unsure how to avoid using global variables. 我正在使用C(而不是C ++),我不确定如何避免使用全局变量。

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. 我对C,它的语法以及如何编写基本应用程序有相当不错的把握,但我不确定构造程序的正确方法。

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? 我很确定总会有至少一些,但对于用C编写的大型游戏和其他应用程序,最好的方法是什么?

Is there any good, open-source software written strictly in C that I could look at? 是否有任何好的,开源软件严格用C编写,我可以看一下? I can't think of any off the top of my head, most of them seem to be in C++. 我无法想到任何问题,其中大多数似乎都是用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. 下面是一个示例,我将在一个简单的API挂钩应用程序中使用全局变量,它只是另一个进程中的DLL。

This application, specifically, hooks API functions used in another application. 具体而言,此应用程序挂钩了另一个应用程序中使用的API函数。 It does this by using WriteProcessMemory to overwrite the call to the original, and make it a call to my DLL instead. 它通过使用WriteProcessMemory覆盖对原始的调用,并使其调用我的DLL来实现。

However, when un hooking the API function, I have to write back the original memory/machine code. 但是,当取消挂起API函数时,我必须回写原始内存/机器代码。

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. 因此,我需要为该机器代码维护一个简单的字节数组,每个API函数都挂钩一个,并且有很多。

// 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. 对不起,如果那令人困惑。

"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. 通过参数传递数据,以便将值定义在一个位置,可能是main()并传递到需要的位置。

  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 . EG: Gbl_MyApp_DeveloperName So all global variables would start with the Gbl_MyApp_ part -- where "MyApp" was something descriptive of your app. 因此,所有全局变量都将从Gbl_MyApp_部分开始 - 其中“MyApp”是描述您的应用程序的内容。
    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). 然后可以定义全局变量并将其限制为该文件(请注意extern关键字)。

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. 创建main()函数中的全局变量,然后将它们作为参数传递给需要它们的函数。

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. 我有一个main()做了很少的事情。 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. 这些是没有参数的功能,因为我正在使用的平台,32位微控制器。 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 ;-) 这是唯一一种不会让我感觉不好的全局变量;-)

在我的大学里学习C的最佳推荐的开源软件是Linux,所以你可以从他们的来源获得例子。

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? 每当你想要一个全局变量时,请想一想:如果我还需要一个这样的变量怎么办?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM