Advertisement

The C keyword 'static'

March 08, 2014

Colin Walls-March 08, 2014

In C and C++, the keyword ‘static’ has two, quite separate uses. In C++, there is even a third use of the keyword. The nuances of these meanings/uses are not always well understood. This article explains those different uses and clarifies their relevance to embedded developers, who are much more likely to be concerned about the details than their desktop programming counterparts.
Here is some outline C++ code which shows static in all its guises:
static int alpha;

  struct beta
  {
    static int gamma;
    ...
  };

  static void delta()
  {
    static int epsilon;
    ...
  }

Static and dynamic storage of variables
In English, the word “static” has a variety of meanings, but they can be summed up by the definition of the adjective: “pertaining to or characterized by a fixed or stationary condition”. In the software world, it generally refers to things that do not change over time. In both cases, the opposite may be “dynamic”.

In C, a variable that is stored statically is assigned an address at build time. It is, therefore, always safe to use a pointer to such a variable. There are two types of dynamic memory in C. Chunks of memory (from the heap) may be requested, typically using the malloc() function or one of its cousins (or the new operator in C++). It is safe to use pointers to data stored in this way, so long as care is taken to avoid their use after the memory is deallocated (using free() or delete). The other possibility is for variables to be stored on the stack. Again, pointers may refer to such variables, as long as they are only used when the variable is in scope.

Inside a function
If you declare a variable inside of a function, it is considered to be an “automatic”, unless you request otherwise. You can explicitly declare a variable thus using the keyword auto (but nobody ever does!). An automatic variable is stored on the stack (though optimization may result in it being allocated to a machine register, particularly if you suggest that by using the keyword register). An automatic variable remains in scope throughout the function/block in which it is declared; in C++ it is accessible for the rest of the function/block, as there is more flexibility in declaration placement.
For example:

  void fun1()
  {
    int var1;
    ...
    {
      int var2;
      ...


Here, both var1 and var2 are automatic and have probably been allocated space on the stack. The scope of each is different: var1 is accessible throughout fun(); var2 is accessible only in the block in which is declared. However, if we modify the code thus:

  void fun2()
  {
    static int var3;
    ...
    {
      static int var4;
      ...


Now, variables var3 and var4 have been allocated addresses at build time. Their scope is the same as their counterparts in the previous example. The big difference is, because the static variables have fixed memory locations, any data they contain persists from one call of a function to another. This is the first meaning of the C keyword 'static'. It has no effect on scope and only affects the storage of a variable.

It is important to appreciate that a function with static variables is not reentrant. This means that problems will arise if the same function code is called from two separated execution threads.


Loading comments...