问题描述
在C,可具有了可以查看文件中的每一个地方的外部静态变量,而内部静态变量仅在函数可见,但具有持久性。
In C you can have external static variables that are viewable every where in the file, while internal static variables are only visible in the function but is persistent
例如:
#include <stdio.h>
void foo_bar( void )
{
static counter = 0;
printf("counter is %d\n", counter);
counter++;
}
int main( void )
{
foo_bar();
foo_bar();
foo_bar();
return 0;
}
的输出将是
counter is 0
counter is 1
counter is 2
我的问题是,为什么你会使用内部静态变量?如果你不想让你的文件的其余部分可见的静态变量不应该的功能真的是在它自己的文件呢?
My question is why would you use an internal static variable? If you don't want your static variable visible in the rest of the file shouldn't the function really be in its own file then?
推荐答案
这混乱通常是有关,因为静态
关键字有两个目的。
This confusion usually comes about because the static
keyword serves two purposes.
当在文件级应用,它控制的的知名度的它的编译单元,而不是的持续时间的对象(可见性和持续时间以外的对象是我用通俗地说在教育会议,ISO标准使用,您可能想了解最终不同的条款,但我发现他们大多数混淆开始学生)。
When used at file level, it controls the visibility of its object outside the compilation unit, not the duration of the object (visibility and duration are layman's terms I use during educational sessions, the ISO standard uses different terms which you may want to learn eventually, but I've found they confuse most beginning students).
在文件级创建的对象已经有了自己的时间凭借的事实,他们在文件级别决定的。在静态
关键字然后只是让他们看不到的连接器。
Objects created at file level already have their duration decided by virtue of the fact that they're at file level. The static
keyword then just makes them invisible to the linker.
在函数内部使用,它控制的持续时间的,不是的的知名度的。能见度已经决定,因为它里面的功能 - 它不能在函数外可见。在这种情况下,静态
关键字,使同时作为文件级对象要创建的对象。
When used inside functions, it controls duration, not visibility. Visibility is already decided since it's inside the function - it can't be seen outside the function. The static
keyword in this case, causes the object to be created at the same time as file level objects.
请注意,从技术上,功能级静态不一定生效的存在,直到该函数首先调用(这可能是有意义的C ++以其构造函数),但我用过的每一个C实现创建它的功能水平静同时作为文件级对象
Note that, technically, a function level static may not necessarily come into existence until the function is first called (and that may make sense for C++ with its constructors) but every C implementation I've ever used creates its function level statics at the same time as file level objects.
另外,虽然我用的是对象,我不是指它在C ++对象的感觉(因为这是一个C的问题)。这只是因为静态
可以应用到文件级的变量或函数,我需要一个包罗万象的词来形容了。
Also, whilst I'm using the word "object", I don't mean it in the sense of C++ objects (since this is a C question). It's just because static
can apply to variables or functions at file level and I need an all-encompassing word to describe that.
功能级别静态仍在使用相当多的 - 他们可以导致多线程程序的麻烦,如果这不是照顾,但是,只要你知道你在做什么(或者你没有线程),它们是在多个函数调用preserve状态最好的方法,同时还提供了封装。
Function level statics are still used quite a bit - they can cause trouble in multi-threaded programs if that's not catered for but, provided you know what you're doing (or you're not threading), they're the best way to preserve state across multiple function calls while still providing for encapsulation.
即使有螺纹,有技巧,你可以在功能做(如线程特定数据的分配的功能范围内),以使它能够发挥作用,避免不必要的暴露函数内部。
Even with threading, there are tricks you can do in the function (such as allocation of thread specific data within the function) to make it workable without exposing the function internals unnecessarily.
我能想到的唯一的其他选择是全局变量,每次路过一个状态变量的功能。
The only other choices I can think of are global variables and passing a "state variable" to the function each time.
在这两种情况下,你揭露功能,以客户的内部运作,使功能依赖于客户端(总是有风险的假设)的良好行为。
In both these cases, you expose the inner workings of the function to its clients and make the function dependent on the good behavior of the client (always a risky assumption).
这篇关于在C内部静态变量,你会用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!