0

When and how are const variables initialized in C/C++? I am curious about particular types:

1) const static member of a class
2) function const local variable
3) const global variable

I mean of course application run time not source code way of initializing them.

3
  • I removed references to C, since C does not come with classes. Commented Mar 27, 2014 at 12:51
  • It has no classes but other two points are valid. Commented Mar 27, 2014 at 13:37
  • Yes, they are... But please avoid asking about "C/C++". There is no such language. C++ was a superset of C in the 80s. We are not in the 80s any more, both C and C++ has been changed numerous times since then. They are not compatible and often behave differently. The different kinds of storage duration is just one example. If you have interest in both languages, it is often best to ask the same question twice, but with different language tabs. Commented Mar 27, 2014 at 14:43

2 Answers 2

5

1) const static member of a class

If it's a simple type initialised with a constant expression, then it's initialised during the static initialisation phase, before any user code is run.

Otherwise, if it's a class type with a non-trivial constructor, or has a non-constant initialiser, then it's initialised during the dynamic initialisation phase. This will happen before any function that's defined in the same translation unit is called; but there are potential deathtraps:

  • It might not happen before other static variables are initialised; you'll get evil bugs if their constructors access it.
  • It might not happen before main begins, if it's defined in a different unit.
  • It might not happen at all, if you never call any function in the same unit.

2) function const local variable

If it's static, then it's initialised the first time the program reaches its definition. If it's automatic, it's initialised every time the program reaches it, and destroyed when it goes out of scope.

3) const global variable

Same as the first. Both have static storage duration, and so are initialised and destroyed according to the same rules.

Notes:

Since you're asking about two different languages: in C, there's no such thing as "dynamic initialisation", and all non-local static variables are initialised before any user code runs.

Being const has no effect on when or how a variable is initialised.

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

2 Comments

Even if it's a simple type, initialization may also take place during the dynamic initialisation phase - if the initializer is not a constant expression.
@AndreyChernyakhovskiy: Thanks, I forgot that detail.
4

Storage duration tells you what rules apply for when the variable in a program is allocated and deallocated. To answer your question is to specify storage duration for each case that you mentioned.

1) const static member of a class  : static storage duration,
                                     simple type - static initialisation,
                                     class with non-trivial constructor - dynamic
                                     initialisation
2) function const local variable   : automatic storage duration ( but static 
                                     duration if it is static const),
                                     automatic variable is initialized each time
                                     the code is run,
                                     static variable is initialized the first
                                     time code is run, 
3) const global variable           : static storage duration
                                     simple type - static initialisation,
                                     class with non-trivial constructor - dynamic
                                     initialisation

Explanation of storage durations:

automatic storage duration

The variable is allocated at the beginning of the enclosing code block and deallocated on end. All non-global variables have this storage duration, except those declared static, extern or thread_local.

static storage duration

The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable exists. All global variables have this storage duration, plus those declared with static or extern.

thread storage duration

The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration. thread_local can appear together with static or extern to adjust linkage. (since C++11)

dynamic storage duration

The variable is allocated and deallocated per request by using dynamic memory allocation functions.

1 Comment

What you provides says about allocation not initialization. It is true that allocation must happen before initialization but it does not have to happen at the same time.

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.