在C/ c++中,全局变量像我的教授认为的那样糟糕吗?


当前回答

我想要反驳这篇文章所提出的观点,即它使多线程更加困难或根本不可能。全局变量是共享状态,但全局变量的替代方法(例如传递指针)也可能共享状态。多线程的问题在于如何正确地使用共享状态,而不是该状态是否恰好通过全局变量或其他方式共享。

大多数情况下,当你执行多线程时,你需要共享一些东西。例如,在生产者-消费者模式中,您可以共享一些包含工作单元的线程安全队列。您可以共享它,因为该数据结构是线程安全的。当涉及到线程安全时,队列是否是全局的完全无关紧要。

在这个线程中隐含的希望是,在不使用全局变量的情况下,将程序从单线程转换为多线程会更容易,这是天真的。是的,全局变量让搬起石头砸自己的脚变得更容易,但搬起石头砸自己的方法有很多。

我并不提倡全局变量,因为其他观点仍然成立,我的观点仅仅是程序中的线程数与变量作用域无关。

其他回答

正如有人在另一篇文章中所说的那样:“在你完全理解这样做的后果之前,不应该打破这样的规则。”

有时候,全局变量是必要的,或者至少是非常有用的(例如,处理系统定义的回调)。另一方面,他们也非常危险,因为所有你被告知的原因。

编程的许多方面可能应该留给专家去做。有时候你需要一把非常锋利的刀。但在你准备好之前,你不能使用它……

全局变量有多糟糕就有多糟糕。

如果您正在创建一个完全封装的程序,则可以使用全局变量。使用全局变量是一种“罪过”,但编程的罪过在很大程度上是哲学上的。

如果你看看洛杉矶。你会看到一种语言,它的变量是全局的。它是不可伸缩的,因为所有的库都只能使用全局变量。

也就是说,如果您有选择,并且可以忽略程序员的哲学,那么全局变量并不是那么糟糕。

Gotos也是,如果你使用得当的话。

最大的“坏”问题是,如果你使用错误,人们尖叫,火星着陆器坠毁,世界爆炸....或者类似的东西。

如果您的代码有可能在最高法院审判期间被严格审查,那么您应该确保避免使用全局变量。

请看这篇文章: 漏洞百出的酒精测试仪代码反映了源代码审查的重要性

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.

伙计,我敢打赌那些开发人员希望他们没有使用全局变量!

不,他们一点都不坏。你需要查看编译器生成的(机器)代码来做出判断,有时使用局部变量比使用全局变量要糟糕得多。还要注意,在局部变量上添加“static”基本上是使其成为全局变量(并产生真正的全局变量可以解决的其他丑陋问题)。“局部全局变量”尤其糟糕。

全局变量还可以让你完全控制内存的使用,这在局部变量中要困难得多。如今,这只在内存相当有限的嵌入式环境中才重要。在假定嵌入式环境与其他环境相同并假定编程规则全面相同之前,需要了解一些事情。

It is good that you question the rules being taught, most of them are not for the reasons you are being told. The most important lesson though is not that this is a rule to carry with you forever, but this is a rule required to honor in order to pass this class and move forward. In life you will find that for company XYZ you will have other programming rules that you in the end will have to honor in order to keep getting a paycheck. In both situations you can argue the rule, but I think you will have far better luck at a job than at school. You are just another of many students, your seat will be replaced soon, the professors wont, at a job you are one of a small team of players that have to see this product to the end and in that environment the rules developed are for the benefit of the team members as well as the product and the company, so if everyone is like minded or if for the particular product there is good engineering reason to violate something you learned in college or some book on generic programming, then sell your idea to the team and write it down as a valid if not the preferred method. Everything is fair game in the real world.

如果你遵循学校或书本上教给你的所有编程规则,那么你的编程生涯将非常有限。你很可能会生存下来,并拥有一个富有成效的事业,但你所能获得的环境的广度和宽度将是极其有限的。如果你知道规则是如何存在的,为什么会存在,并且能够捍卫它,那很好,如果你的理由只是“因为我的老师这么说”,那就不太好了。

请注意,这样的话题经常在工作场所被争论,并且将继续存在,随着编译器和处理器(以及语言)的发展,这些类型的规则也在发展,如果不捍卫你的立场,可能会被持不同意见的人教训一课,你就不会前进。

与此同时,那个说话声音最大或拿着最大棍子的人说什么,你就做什么(直到你是那个喊得最大、拿着最大棍子的人)。

迟早,您将需要更改该变量的设置方式或访问它时发生的情况,或者您只需要查找它被更改的位置。

实际上,不使用全局变量总是更好。只需编写大坝获取和设置方法,并在一天、一周或一个月后当你需要它们时通知你。