Consider the following function that is called periodically:
void func(void)
{
const int myConstArray[4] = {1, 2, 3, 4};
// Doing some operations including the above array
}
If one changes the myConstArray
from const
to static const
, does that change anything at all on how the code compiles?
CodePudding user response:
Changing const
to static const
changes the storage duration of the object. When the array is declared const myConstArray[4] = {1, 2, 3, 4};
inside a function, it has automatic storage duration, meaning memory is reserved for it from the time execution of its associated block starts until that execution of the block ends. Changing the declaration to static const myConstArray[4] = {1, 2, 3, 4};
gives it static storage duration, meaning memory is reserved for it for the entire program.
Now, if the program does not make any use of the longer storage duration, then the compiler does not have to do anything differently. Consider this code:
int foo(int x)
{
static const int Table[] = { 1, 5, 3, 6 };
return Table[x];
}
In this code, the compiler can see Table
is only used to look up a value. Therefore, it does not need to exist when the function is not executing—the program behaves the same whether the memory is reserved for Table
or not, so it does not matter if it is declared static
or not.
Compare that to this code:
int foo(int x)
{
static const int Table[] = { 1, 5, 3, 6 };
bar(Table);
return Table[x];
}
Here the function passes the address of Table
to bar
. The compiler does not know what bar
is going to do with that address. bar
might store the address, to be used later in the program by some other routine. In this case, it matters whether Table
is static
or not. If Table
is not static
, its lifetime does not extend beyond the execution of foo
, so its address should not be used later (if it is, the program behavior is not defined by the C standard). This means the compiler may use stack space for Table
. However, if Table
is declared with static
, its address can be used after execution of foo
, so the compiler must reserve memory for it even after execution of foo
ends.
Finally, consider this code:
int foo(int x)
{
const int Table[] = { 1, 5, 3, 6 };
bar(Table);
return Table[x];
}
Since Table
is not declared static
, each time foo
is called, the Table
in it is a new object in the semantics of the C standard. Further, all different objects must have different addresses (except the subobject at the start of a containing object, such as an array and its first element or a structure and its first member). So, if bar
calls foo
recursively, a second Table
is created and bar
is called again. Since the two Table
objects must be different, bar
must be passed a different address. This means that, with Table
not declared static
, the compiler may be compelled to create a new instance of it on the stack each time foo
is called. To avoid this, it is preferable to declare Table
with static
.
CodePudding user response:
variable declared as static is a constant, for the entire program, and is stored in the data segment. Compilers implement this by having a section that has the values in them.(which will increase the response speed of the program).
variables not declared as static typically live on the stack ( constant for the lifetime of the function) and must be initialized every time the variable's scope is entered.