在C/ c++中,全局变量像我的教授认为的那样糟糕吗?
当前回答
绝对不是。但是滥用它们……这很糟糕。
出于某种原因而无意识地删除它们只是……盲目的。除非您知道其优点和缺点,否则最好避免使用全局变量,并按照您学到的方法进行操作,但全局变量并没有什么隐含的错误。当你了解了利弊后,最好自己做决定。
其他回答
迟早,您将需要更改该变量的设置方式或访问它时发生的情况,或者您只需要查找它被更改的位置。
实际上,不使用全局变量总是更好。只需编写大坝获取和设置方法,并在一天、一周或一个月后当你需要它们时通知你。
问题不在于它们有多坏,而在于它们有多危险。他们有自己的优点和缺点,在某些情况下,他们要么是最有效的,要么是完成特定任务的唯一方法。然而,它们很容易被误用,即使你采取措施总是正确使用它们。
一些优点:
可以从任何函数访问。 可以从多个线程访问。 在项目结束前都不会超出范围。
一些缺点:
可以从任何函数访问,而不需要显式地作为参数拖入和/或记录。 不是线程安全的。 污染全局名称空间并可能导致名称冲突,除非采取措施防止这种情况。
请注意,如果你愿意的话,我列出的前两个优点和前两个缺点是完全相同的事情,只是措辞不同。这是因为全局变量的特性确实是有用的,但使它们有用的特性正是它们所有问题的根源。
一些问题的潜在解决方案:
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.
它们是好是坏取决于你如何使用它们。大多数人倾向于不好地使用它们,因此对它们普遍持谨慎态度。如果使用得当,它们可以成为一大福音;然而,如果使用不当,它们可能会在你最意想不到的时候回来咬你一口。
从一个好的角度来看,它们本身并不坏,但它们导致了糟糕的设计,并且会成倍地增加糟糕设计的影响。
即使你不打算使用它们,知道如何安全使用它们并选择不使用,也比不使用它们因为你不知道如何安全使用它们要好。如果您发现自己处于需要维护依赖于全局变量的预先存在的代码的情况下,如果您不知道如何正确使用它们,那么您可能会遇到困难。
是的,但是在停止使用全局变量的代码并开始编写使用全局变量的代码之前,您不会招致全局变量的成本。但成本依然存在。
换句话说,这是一种长期的间接成本,因此大多数人认为这并不坏。
全局变量的使用实际上取决于需求。它的优点是减少了重复传递值的开销。
但是你的教授是对的,因为它会引起安全问题,所以应该尽可能避免使用全局变量。全局变量还会产生一些问题,有时难以调试。
例如:-
变量值在运行时被修改的情况。此时,很难确定是哪部分代码在什么条件下修改了它。
当涉及到配置时,全局是很好的。当我们希望我们的配置/更改对整个项目产生全局影响时。
因此,我们可以改变一个配置,并将更改指向整个项目。但我必须警告你,你必须非常聪明地使用全局变量。
推荐文章
- 如何构建和使用谷歌TensorFlow c++ api
- C“int”的大小是2字节还是4字节?
- 多维数组如何在内存中格式化?
- printf()和puts()在C语言中的区别是什么?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 有效,但毫无价值的语法在开关情况下?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- 有一个好的Valgrind Windows的替代品吗?
- gcc在哪里查找C和c++头文件?
- 为什么我们需要require require ?