C语言 为什么以及何时在 C 编程中使用静态结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7259830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:32:39  来源:igfitidea点击:

Why and when to use static structures in C programming?

cstaticstruct

提问by kutty

I have seen static structure declarations quite often in a driver code I have been asked to modify.

我经常在要求我修改的驱动程序代码中看到静态结构声明。

I tried looking for information as to why structsare declared static and the motivation of doing so.

我试图寻找有关为什么structs声明为静态以及这样做的动机的信息。

Can anyone of you please help me understand this?

你们中的任何人都可以帮助我理解这一点吗?

回答by fvu

The statickeyword in C has several effects, depending on the context it's applied to.

staticC 中的关键字有多种效果,具体取决于它所应用的上下文。

  • when applied to a variable declared inside a function, the value of that variable will be preserved between function calls.
  • when applied to a variable declared outside a function, or to a function, the visibility of that variable or function is limited to the "translation unit"it's declared in - ie the file itself. For variables this boils down to a kind of "locally visible global variable".
  • 当应用于函数内部声明的变量时,该变量的值将在函数调用之间保留。
  • 当应用于在函数外部声明的变量或函数时,该变量或函数的可见性仅限于它在其中声明的“翻译单元”——即文件本身。对于变量,这归结为一种“局部可见的全局变量”。

Both usages are pretty common in relatively low-level code like drivers.

这两种用法在驱动程序等相对低级的代码中都很常见。

The former, and the latter when applied to variables, allow functions to retain a notion of state between calls, which can be very useful, but this can also cause all kinds of nasty problems when the code is being used in any context where it is being used concurrently, either by multiple threads or by multiple callers. If you cannot guarantee that the code will strictly be called in sequence by one "user", you can pass a kind of "context" structure that's being maintained by the caller on each call.

前者和后者应用于变量时,允许函数在调用之间保留状态概念,这非常有用,但是当代码在任何上下文中使用时,这也可能导致各种令人讨厌的问题被多个线程或多个调用者同时使用。如果你不能保证代码会被一个“用户”严格按顺序调用,你可以传递一种由调用者在每次调用时维护的“上下文”结构。

The latter, applied to functions, allows a programmer to make the function invisible from outside of the module, and it MAYbe somewhat faster with some compilers for certain architectures because the compiler knows it doesn't have to make the variable/function available outside the module - allowing the function to be inlined for example.

后者应用于函数,允许程序员使函数从模块外部不可见,并且对于某些体系结构的某些编译器可能会更快一些,因为编译器知道它不必使变量/函数在外部可用模块 - 例如允许内联函数。

回答by Jens

Something that apparently all other answers seem to miss: staticis and specifies also a storage durationfor an object, along with automatic(local variables) and allocated(memory returned by malloc and friends).

显然所有其他答案似乎都遗漏了一些东西:staticis 并且还指定了对象的存储持续时间,以及自动(局部变量)和已分配(由 malloc 和朋友返回的内存)。

Objects with static storage duration are initialized before main() starts, either with the initializer specified, or, if none was given, as if 0 had been assigned to it (for structs and arrays this goes for each member and recursively).

具有静态存储持续时间的对象在 main() 开始之前被初始化,或者使用指定的初始化程序,或者,如果没有给出,就好像 0 已分配给它(对于结构和数组,这适用于每个成员并递归)。

The second property staticsets for an identifier, is its linkage, which is a concept used at link time and tells the linker which identifiers refer to the same object. The statickeyword makes an identifier have internal linkage, which means it cannot refer to identifiers of the same name in another translation unit.

第二属性static集的标识符,是它的联动,这是在链接时使用的概念,并告诉哪个标识符指代相同的对象中的链接器。所述static关键字使得一个标识符具有内部连接的,这意味着它不能指代相同的名称的标识符在另一翻译单元。

And to be pedantic about all the sloppy answers I've read before: a static variable can not be referenced everyhere in the file it is declared. Its scope is only from its declaration (which can be between function definitions) to the end of the source file--or even smaller, to the end of the enclosing block.

并且对于我之前读过的所有草率答案要迂腐:不能在声明的文件中的每个地方都引用静态变量。它的范围仅从它的声明(可以在函数定义之间)到源文件的末尾——甚至更小,到封闭块的末尾。

回答by Mihai Maruseac

If you declare a variable as being static, it is visible only in that translation unit(if globally declared) or retains its value from call to call (if declared inside a function).

如果将变量声明为staticis ,则它仅在该翻译单元中可见(如果全局声明)或在调用之间保留其值(如果在函数内部声明)。

In your case I guess it is the first case. In that case, probably the programmer didn't want the structure to be visible from other files.

在你的情况下,我想这是第一种情况。在那种情况下,程序员可能不希望结构从其他文件中可见。

回答by Brandon E Taylor

The staticmodifier for the structlimits the scope of visibility of the structure to the current translation unit (i.e. the file).

static修饰符struct将结构的可见范围限制为当前翻译单元(即文件)。

NOTE: This answer assumes (as other responders have indicated) that your declaration is not within a function.

注意:此答案假定(正如其他响应者指出的那样)您的声明不在函数内。