Chapter 6 discussed the storage-class specifier static
. A static
local variable in a function definition exists for the program’s duration but is visible only in the function’s body.
Performance Tip 7.1
We can apply static to a local array declaration so that it’s not created and initialized each time the program calls the function and is not destroyed each time the function terminates. This can improve performance, especially when using large arrays.
A program initializes static
local array
s when their declarations are first encountered. If a static array
is not initialized explicitly by you, each element of that array
is initialized to zero by the compiler when the array
is created. Recall that C++ does not perform such default initialization for automatic variables.
Figure 7.12 demonstrates function staticArrayInit
(lines 24–40) with a static
local array
(line 27) and function automaticArrayInit
(lines 43–59) with an automatic local array
(line 46).
1 // Fig. 7.12: fig07_12.cpp
2 // static array initialization and automatic array initialization.
3 #include <iostream>
4 #include <array>
5 using namespace std;
6
7 void staticArrayInit(); // function prototype
8 void automaticArrayInit(); // function prototype
9 const size_t arraySize = 3;
10
11 int main()
12 {
13 cout << "First call to each function:
";
14 staticArrayInit();
15 automaticArrayInit();
16
17 cout << "
Second call to each function:
";
18 staticArrayInit();
19 automaticArrayInit();
20 cout << endl;
21 } // end main
22
23 // function to demonstrate a static local array
24 void staticArrayInit( void )
25 {
26 // initializes elements to 0 first time function is called
27 static array< int, arraySize > array1; // static local array
28
29 cout << "
Values on entering staticArrayInit:
";
30
31 // output contents of array1
32 for ( size_t i = 0; i < array1.size(); ++i )
33 cout << "array1[" << i << "] = " << array1[ i ] << " ";
34
35 cout << "
Values on exiting staticArrayInit:
";
36
37 // modify and output contents of array1
38 for ( size_t j = 0; j < array1.size(); ++j )
39 cout << "array1[" << j << "] = " << ( array1[ j ] += 5 ) << " ";
40 } // end function staticArrayInit
41
42 // function to demonstrate an automatic local array
43 void automaticArrayInit( void )
44 {
45 // initializes elements each time function is called
46 array< int, arraySize > array2 = { 1, 2, 3 }; // automatic local array
47
48 cout << "
Values on entering automaticArrayInit:
";
49
50 // output contents of array2
51 for ( size_t i = 0; i < array2.size(); ++i )
52 cout << "array2[" << i << "] = " << array2[ i ] << " ";
53
54 cout << "
Values on exiting automaticArrayInit:
";
55
56 // modify and output contents of array2
57 for ( size_t j = 0; j < array2.size(); ++j )
58 cout << "array2[" << j << "] = " << ( array2[ j ] += 5 ) << " ";
59 } // end function automaticArrayInit
First call to each function:
Values on entering staticArrayInit:
array1[0] = 0 array1[1] = 0 array1[2] = 0
Values on exiting staticArrayInit:
array1[0] = 5 array1[1] = 5 array1[2] = 5
Values on entering automaticArrayInit:
array2[0] = 1 array2[1] = 2 array2[2] = 3
Values on exiting automaticArrayInit:
array2[0] = 6 array2[1] = 7 array2[2] = 8
Second call to each function:
Values on entering staticArrayInit:
array1[0] = 5 array1[1] = 5 array1[2] = 5
Values on exiting staticArrayInit:
array1[0] = 10 array1[1] = 10 array1[2] = 10
Values on entering automaticArrayInit:
array2[0] = 1 array2[1] = 2 array2[2] = 3
Values on exiting automaticArrayInit:
array2[0] = 6 array2[1] = 7 array2[2] = 8
Function staticArrayInit
is called twice (lines 14 and 18). The static
local array1
is initialized to zero by the compiler the first time the function is called. The function prints the array
, adds 5 to each element and prints the array
again. The second time the function is called, the static array
contains the modified values stored during the first function call.
Function automaticArrayInit
also is called twice (lines 15 and 19). Automatic local array2
’s elements are initialized (line 46) with the values 1, 2 and 3. The function prints the array
, adds 5 to each element and prints the array
again. The second time the function is called, the array
elements are reinitialized to 1, 2 and 3. The array
has automatic storage duration, so the array
is recreated and reinitialized during each call to automaticArrayInit
.