3

By coincidence I stumbled over "The New C Standard" from Derek M. Jones, where he comments this sentence from the C99 standard regarding "storage duration of objects":

There are three storage durations: static, automatic, and allocated.

His comment:

One of the uses of the overworked keyword static is to denote objects that have static storage duration (there are other ways of denoting this storage duration).

The text marked bold is what irritates me. I've read this thread and this one, but it doesn't clarify it to me. Maybe my research is incomplete or I misunderstand his point...

I understand that the static keyword is used to limit access to the file where the variable is declared, and I understand that those variables' lifetime is "unlimited" throughout the runtime of the program.
I also understand that some getter() functions should be used to access those variables from other modules instead of tampering with extern declarations. And I understand the argument from @amon that

"static makes code untestable"

Btw, to overcome this, I've seen solutions where some construct like

#ifdef TESTING_ACTIVE
    #define STATIC 
#else
    #define STATIC static
#endif

was introduced, having a compiler switch "-DTESTING_ACTIVE" for testing purposes.

Michael Barr says in his Embedded C Coding Standard:

The static keyword shall be used to declare all functions and variables that do not need to be visible outside of the module in which they are declared.

So... what exactly is Derek's problem with static and what are those "other ways of denoting this storage duration" in C99?

mic
  • 147

2 Answers2

5

The context of this paragraph is explaining storage duration, not the keyword static. The parenthetical statement in question is just referring to the fact that there are many things with static storage duration that don't use the keyword static.

For example, any file scope variable has static storage duration, despite not being marked with the keyword static:

int x;        // static storage duration, external linkage
static int y; // static storage duration, internal linkage

Static storage duration in this case is "denoted" by the variable's mere existence. Functions and string literals also have static storage duration without using the static keyword (maybe other things too).

In fact, the only time static actually means "static storage duration" is for local variables with no linkage:

void foo() {
    int x;        // automatic storage duration
    static int y; // static storage duration
}

Giving static storage duration to local variables is in fact the original function of the static keyword. The decision to reuse the same keyword for an unrelated purpose (that is, to modify linkage of file-scope variables) has caused a lot of confusion and is often considered to have been a mistake (hence the "overworked" comment in the original quote).

trent
  • 252
0

The main problem with static is that it destroys reentrancy - it’s why you can’t "nest” calls to strtok. You only have a single instance of that object over the lifetime of the program. It is created and initialized once at program startup, and persists until program exit.

It can be confusing to read code like

void foo( void )
{
  static int x = 5;
  ...
}

because x only gets initialized to 5 once, before foo is called, and then retains whatever value was last written to it between calls.

It’s all the heartburn of a global variable with extra confusion about scope and initialization thrown in for good measure.

John Bode
  • 11,004
  • 1
  • 33
  • 44