C - 程序结构(避免全局变量、包含等)
我正在使用 C (不是 C++),并且不确定如何避免使用全局变量。
我对 C、它的语法以及如何编写基本应用程序有相当不错的掌握,但我不确定构建程序的正确方法。
真正的大型应用程序如何避免使用全局变量?我很确定总是需要至少一些,但是对于用 C 编写的大型游戏和其他应用程序,最好的方法是什么?
有没有什么好的、严格用 C 语言编写的开源软件可以看一下?我无法立即想到任何内容,其中大多数似乎都是用 C++ 编写的。
谢谢。
编辑
下面是我在简单的 API 挂钩应用程序中使用全局变量的示例,该应用程序只是另一个进程内的 DLL。
具体而言,该应用程序挂钩另一个应用程序中使用的 API 函数。它通过使用 WriteProcessMemory 覆盖对原始的调用并使其成为对我的 DLL 的调用来实现此目的。
然而,当取消挂钩API函数时,我必须写回原始内存/机器代码。
因此,我需要为该机器代码维护一个简单的字节数组,每个被挂钩的 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 );
抱歉,如果这令人困惑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用静态变量。如果函数需要在调用之间记住某些内容,请使用 this 而不是全局变量。示例:
通过参数传递数据,以便将值定义在一个地方,也许是
main()
并传递到需要它的地方。如果所有其他方法都失败,请继续使用全局,但尝试减轻潜在的问题。
Gbl_MyApp_DeveloperName
。因此,所有全局变量都以Gbl_MyApp_
部分开头 - 其中“MyApp”是对您的应用的描述。extern
关键字)。Use static variables. If a function needs to remember something between calls, use this versus global variables. Example:
Pass data via parameters, so that the value is defined one place, maybe
main()
and passed to where it is needed.If all else fails, go ahead and use a global but try and mitigate potential problems.
Gbl_MyApp_DeveloperName
. So all global variables would start with theGbl_MyApp_
part -- where "MyApp" was something descriptive of your app.extern
keyword).全局变量有一些有效的用途。学校开始教导他们是邪恶的,以防止程序员偷懒和过度使用它们。如果您确定全球确实需要这些数据,那么就使用它们。鉴于您关心做好工作,我认为您自己的判断不会错得太离谱。
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.
这很简单:只需不要使用它们即可。在
main()
函数中创建全局变量,然后将它们作为参数传递给需要它们的函数。It's easy: simply don't use them. Create what would have been the global variables in your
main()
function, and then pass them from there as parameters to the functions that need them.在我的大学中,最推荐的学习 C 语言的开源软件是 Linux,因此您可以从其源代码中获取示例。
The best-recomended open-source software to learn C in my college had been Linux so you can get examples from their source.
我认为同样,全局变量很糟糕,会降低可读性并增加错误可能性和其他问题。
我将解释我的例子。我有一个 main() 做了很少的事情。大部分操作来自定时器和外部中断。由于我使用的平台是 32 位微控制器,这些函数没有参数。我无法传递参数,此外,这些中断和计时器是异步的。最简单的方法是全局中断填充缓冲区,该缓冲区在计时器内部解析,解析的信息在其他计时器中使用、存储和发送。
好吧,我可以在主函数中拉出一个中断标志并进行处理,但是在执行关键软件时,这种方式不是一个好主意。
这是唯一一种不会让我感觉不好的全局变量;-)
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?