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 structs
are declared static and the motivation of doing so.
Can anyone of you please help me understand this?
Something that apparently all other answers seem to miss:
static
is and specifies also a storage duration for an object, along with automatic (local variables) and allocated (memory returned by malloc and friends).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).
The second property
static
sets 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. Thestatic
keyword makes an identifier have internal linkage, which means it cannot refer to identifiers of the same name in another translation unit.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.
The
static
keyword in C has several effects, depending on the context it's applied to.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 MAY be 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.
The
static
modifier for thestruct
limits the scope of visibility of the structure to the current translation unit (i.e. the file).NOTE: This answer assumes (as other responders have indicated) that your declaration is not within a function.
struct variable
For a struct variable like
static struct S s;
, this has been widely discussed at: What does "static" mean in C?struct definition: no effect:
is the exact same as:
so never use it. GCC 4.8 raises a warning if you do it.
This is because struct definitions have no storage, and do no generate symbols in object files like variables and functions. Just try compiling and decompiling:
with:
and you will see that there is no
S
symbol, but there is ani
symbol.The compiler simply uses definitions to calculate the offset of fields at compile time.
This is struct definitions are usually included in headers: they won't generate multiple separate data, even if included multiple times.
The same goes for
enum
.C++ struct definition: deprecated in C++11
C++11 N3337 standard draft Annex C 7.1.1:
See also: https://stackoverflow.com/a/31201984/895245
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).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.