在C/ c++中,全局变量像我的教授认为的那样糟糕吗?
当前回答
我想要反驳这篇文章所提出的观点,即它使多线程更加困难或根本不可能。全局变量是共享状态,但全局变量的替代方法(例如传递指针)也可能共享状态。多线程的问题在于如何正确地使用共享状态,而不是该状态是否恰好通过全局变量或其他方式共享。
大多数情况下,当你执行多线程时,你需要共享一些东西。例如,在生产者-消费者模式中,您可以共享一些包含工作单元的线程安全队列。您可以共享它,因为该数据结构是线程安全的。当涉及到线程安全时,队列是否是全局的完全无关紧要。
在这个线程中隐含的希望是,在不使用全局变量的情况下,将程序从单线程转换为多线程会更容易,这是天真的。是的,全局变量让搬起石头砸自己的脚变得更容易,但搬起石头砸自己的方法有很多。
我并不提倡全局变量,因为其他观点仍然成立,我的观点仅仅是程序中的线程数与变量作用域无关。
其他回答
我想用另一个问题来回答这个问题:你使用singeltons吗/ singeltons不好吗?
因为(几乎所有)单变量的使用都是一个美化的全局变量。
如果您的代码有可能在最高法院审判期间被严格审查,那么您应该确保避免使用全局变量。
请看这篇文章: 漏洞百出的酒精测试仪代码反映了源代码审查的重要性
There were some problems with the style of the code that were identified by both studies. One of the stylistic issues that concerned the reviewers was the extensive use of unprotected global variables. This is considered poor form because it increases the risk that the program state will become inconsistent or that values will be inadvertently modified or overwritten. The researchers also expressed some concern about the fact that decimal precision is not maintained consistently throughout the code.
伙计,我敢打赌那些开发人员希望他们没有使用全局变量!
问题不在于它们有多坏,而在于它们有多危险。他们有自己的优点和缺点,在某些情况下,他们要么是最有效的,要么是完成特定任务的唯一方法。然而,它们很容易被误用,即使你采取措施总是正确使用它们。
一些优点:
可以从任何函数访问。 可以从多个线程访问。 在项目结束前都不会超出范围。
一些缺点:
可以从任何函数访问,而不需要显式地作为参数拖入和/或记录。 不是线程安全的。 污染全局名称空间并可能导致名称冲突,除非采取措施防止这种情况。
请注意,如果你愿意的话,我列出的前两个优点和前两个缺点是完全相同的事情,只是措辞不同。这是因为全局变量的特性确实是有用的,但使它们有用的特性正是它们所有问题的根源。
一些问题的潜在解决方案:
Consider whether they're actually the best or most efficient solution for the problem. If there are any better solutions, use that instead. Put them in a namespace [C++] or singleton struct [C, C++] with a unique name (a good example would be Globals or GlobalVars), or use a standardised naming convention for global variables (such as global_[name] or g_module_varNameStyle (as mentioned by underscore_d in the comments)). This will both document their use (you can find code that uses global variables by searching for the namespace/struct name), and minimise the impact on the global namespace. For any function that accesses global variables, explicitly document which variables it reads and which it writes. This will make troubleshooting easier. Put them in their own source file and declare them extern in the associated header, so their use can be limited to compilation units that need to access them. If your code relies on a lot of global variables, but each compilation unit only needs access to a handful of them, you could consider sorting them into multiple source files, so it's easier to limit each file's access to global variables. Set up a mechanism to lock and unlock them, and/or design your code so that as few functions as possible need to actually modify global variables. Reading them is a lot safer than writing them, although thread races may still cause problems in multithreaded programs. Basically, minimise access to them, and maximise name uniqueness. You want to avoid name collisions and have as few functions as possible that can potentially modify any given variable.
它们是好是坏取决于你如何使用它们。大多数人倾向于不好地使用它们,因此对它们普遍持谨慎态度。如果使用得当,它们可以成为一大福音;然而,如果使用不当,它们可能会在你最意想不到的时候回来咬你一口。
从一个好的角度来看,它们本身并不坏,但它们导致了糟糕的设计,并且会成倍地增加糟糕设计的影响。
即使你不打算使用它们,知道如何安全使用它们并选择不使用,也比不使用它们因为你不知道如何安全使用它们要好。如果您发现自己处于需要维护依赖于全局变量的预先存在的代码的情况下,如果您不知道如何正确使用它们,那么您可能会遇到困难。
安全并不意味着任何一个可以操纵变量声明的全局,为这一解释这个例子中如果你有平衡作为一个全局变量在你们银行程序用户函数可以操纵以及银行职员也可以操纵这这是一个问题只有用户应该考虑到只读和退出函数,而是银行的职员可以添加量当用户亲自给的现金在桌子上。这就是它的运作方式
全局变量的问题是,由于每个函数都可以访问这些变量,因此越来越难以确定哪些函数实际读写这些变量。
为了理解应用程序是如何工作的,您几乎必须考虑修改全局状态的每个函数。这是可以做到的,但随着应用程序的增长,它将变得越来越困难,几乎不可能(或至少完全浪费时间)。
如果不依赖全局变量,则可以根据需要在不同函数之间传递状态。这样,您就有更好的机会理解每个函数的功能,因为您不需要考虑全局状态。