0

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?

2
  • 1
    That's what godbolt is for. Commented Apr 15, 2022 at 7:16
  • Yes, I forgot the type. Commented Apr 15, 2022 at 15:08

1 Answer 1

1

Changing const to static const changes the storage duration of the object. When the array is declared const int 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 int 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. So in practice, the variable inside of a function that is declared const (but not static), can be still placed in the data memory segment instead of program memory? Does this mean that all "local" constants should be set as static const to save RAM?
@ŁukaszPrzeniosło: Often it will not make a difference, but there are some situations where it is preferable to declare constants as static. I added an example to the answer about that. I cannot think of an example where there is a benefit to not declaring a constant static outside of contrived situations where multiple instances of an object are needed for some exotic reason. So one should probably default to using static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.