简体   繁体   中英

When is const variable initialized

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM