是否有使用全局变量,而不是指针的优势呢? [英] Is there an advantage in using global variables instead of pointers?

查看:199
本文介绍了是否有使用全局变量,而不是指针的优势呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个code-文件(6K线),完全适用于全局变量和拥有很多的操作无效FUNC(无效),但功能改变了很多全局变量。有些全局变量与结构和函数指针的结构结构,所以他们是巨大的。

I encountered a code-file(6k lines), which completely works on global variables and has a lot of functions that operate void func(void) but alter a lot of the global variables. Some of the globals are structs with structs of structs and pointers to functions, so they are HUGE.

在code是难读,难维护和难以测试的,因为功能没有真正的接口说明,但工作的无处不在。而且它已经用在控制器上相当长的一段时间,所以这不只是一些垃圾有人为自己codeD。

The code is hard to read, hard to maintain and hard to test, because the functions have no real interface description but work "everywhere". And it is already used on controllers for quite some time, so this is not just some garbage someone coded for himself.

现在我想知道有没有理由code这种方式(完全工作的全局,而不是使用指针)?它是有可能更快或更小或有其他好处,可以使这比用指针结构良好的功能更加有用吗?因为这违背了一切,我了解到目前为止编码。

Now I am wondering are there reasons to code this way (completely working on globals instead of using pointers)? Is it possibly faster or smaller or are there other benefits that could make this more useful than well-structured functions with pointers? Because this goes against everything I learned about coding so far.

推荐答案

首先,从来就没有理由的任何应用程序中使用全局,非恒定的变量。全球性的,如在提供给整个程序。

First of all, there is never a reason to use global, non-constant variables in any application. Global, as in available to the whole program.

但可能有一些情况下,静态文件作用域变量意义。请注意,静态关键字块被全局变量,它只是提供给的翻译单元的(即C文件加上包括标题)的地方声明。

There might however be some cases where static file scope variables make sense. Note that the static keyword blocks the variable from being global, it is only available to the translation unit (meaning c file plus included headers) where it is declared.

一些例子,这样的设计意义:

Some examples where such designs make sense:


  • 在设计自主code模块(叫他们班,ADT的或其他),你可以肯定,只有一个实例存在,它可能是方便使用静态文件范围变量作为穷人的私人。只要你提供某种API调用者,你将几乎总是需要专用内部变量。

  • When designing autonomous code modules (call them classes, ADTs or whatever) and you can be sure that only one instance exists, it might be convenient to use static file scope variables as "poor man's private". As soon as you provide some sort of API to the caller, you will almost always have a need for private, internal variables.

这种做法在嵌入式系统中的单核心编程常见。没有那么在桌面编程,其中重入code是重要得多。

This practice common in embedded systems single-core programming. Less so in desktop programming, where re-entrant code is much more important.

特殊情况。当共享调用者和中断服务程序或一个回调函数之间的变量,参数传递可能不是一个选项。然后,你没有其他选择,只能使用静态文件作用域变量。

Special cases. When sharing variables between the caller and an interrupt service routine or a callback function, parameter passing might not be an option. And then you have no other choice but to use static file scope variables.

(这样的变量也应被声明为挥发性来prevent编译器优化的错误,在情况下,编译器不会意识到ISR /回调执行。)

(Such variables should also be declared as volatile to prevent compiler optimizer bugs, in cases where the compiler doesn't realize that the ISR/callback is executed.)

为什么你会不会写一个文件大于2K LOC是因为你正在写一些库,由于某种原因需要传递为单个文件的唯一原因。例如,如果你正在写的标准C库的更复杂的库文件中的一个。

The only reason why you would ever write a file larger than 2k LOC is because you are writing some sort of library that for some reason needs to be delivered as a single file. For example, if you are writing one of the more complex library files of the standard C library.

如果这不是你在做什么,还有的从不的任何借口写6K LOC文件,期。别说复杂的数据类型来填充它。

If that is not what you are doing, there is never any excuse to write a 6k LOC file, period. Let alone to fill it with complex data types.

即使写东西像上面的第一种情况,没有什么在几个拆分文件阻止你。大多数情况下,它实际上是main.c文件的升级超越2K LOC,那应该很容易分裂。

Even when writing something like the first case above, there is nothing stopping you from splitting the file in several. Most often it is actually the "main.c" that escalates beyond 2k LOC, and that one should be easy to split.

所以,很遗憾,我怀疑你是维护code是一个初学者写的。这是不寻常,pretty多少大家谁的作品作为专业的程序员在他们的第一个5年左右写这样的怪物。因为声音的程序设计是你主要是通过吸取经验。

So unfortunately, I suspect that the code you are maintaining is written by a beginner. It is not unusual, pretty much everyone who works as professional programmers write such monsters during their first 5 years or so. Because sound program design is something you mostly learn through experience.

这篇关于是否有使用全局变量,而不是指针的优势呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆