Let’s look at how to create the three different kinds of static duration variables; then we can go on to examine their properties. To create a static duration variable with external linkage, you declare it outside any block. To create a static duration variable with internal linkage, you declare it outside any block and use the static storage class modifier. To create a static duration variable with no linkage, you declare it inside a block, using the static modifier. The following code fragment shows these three variations:

...

int global = 1000;           // static duration, external linkage

static int one_file = 50;    // static duration, internal linkage

int main()

{

...

}

void funct1(int n)

{

    static int count = 0;  // static duration, no linkage

    int llama = 0;

...

}

void funct2(int q)

{

...

}

As stated previously, all the static duration variables (global, one_file, and count, in this example) persist from the time the program begins execution until it terminates. The variable count, which is declared inside funct1(), has local scope and no linkage, which means it can be used only inside the funct1() function, just like the automatic variable llama. But unlike llama, count remains in memory even when the funct1() function is not being executed. Both global and one_file have file scope, meaning they can be used from the point of declaration until the end of the file. In particular, both can be used in main(), funct1(), and funct2(). Because one_file has internal linkage, it can be used only in the file containing this code. Because global has external linkage, it also can be used in other files that are part of the program.

All static duration variables share the following initialization feature: An uninitialized static variable has all its bits set to 0. Such a variable is said to be zero-initialized.

Table 9.1 summarizes the storage class features as used in the pre-namespace era. Next, we’ll examine the static duration varieties in more detail.

Table 9.1. The Five Kinds of Variable Storage

Note that the keyword static has somewhat different meanings in the two uses shown in Table 9.1. When used with a local declaration to indicate a static variable with no linkage, static indicates the kind of storage duration. When used with a declaration outside of a block, static indicates internal linkage; the variable already has static duration. One might term this keyword overloading, with the precise meaning determined by context.

Initializing Static Variables

Static variables may be zero-initialized, they may undergo constant expression initialization, and they may undergo dynamic initialization. As you may have surmised, zero-initialization means setting the variable to the value zero. For scalar types, the zero is type cast to the appropriate type. For example, the null pointer, which is represented by 0 in C++ code, may have a nonzero internal representation, so a pointer variable would be initialized to that value. Structure members are zero-initialized, and any padding bits are set to zero.

Zero-initialization and constant-expression initialization collectively are called static initialization. This means the variable is initialized when the compiler processes the file (or translation unit). Dynamic initialization means the variable is initialized later.

So what determines which form of initialization takes place? First of all, all static variables are zero-initialized, whether or not any initialization is indicated. Next, if the variable is initialized using a constant expression that the compiler can evaluate solely from the file contents (including included header files), it can perform constant-expression initialization. The compiler is prepared to do simple calculations if needed. If there’s not enough information at this time, the variable will be dynamically initialized. Consider the following:

#include

int x;                              // zero-initialization

int y = 5;                          // constant-expression initialization

long z = 13 * 13;                   // constant-expression initialization

const double pi = 4.0 * atan(1.0);  // dynamic initialization

First, x, y, z, and pi are zero-initialized. Then the compiler evaluates the constant expressions and initializes y and z to 5 and 169, respectively. But initializing pi requires calling the atan() function, and this has to wait until the function is linked and the program executes.

A constant expression is not limited to arithmetic expressions using literal constants. It can, for example, use the sizeof operator:

int enough = 2 * sizeof (long) + 1;  // constant expression initialization

C++11 introduces a new keyword, constexpr, to expand the options for creating constant expressions; this is one of the new C++11 features that this book does not pursue further.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги