When is const considered truly const?

When reading some questions and answers in stackoverflow, I come across this question

I tried to understand this, but the answers were very difficult to understand, especially terms such as

  • static storage duration

  • expression cannot be evaluated during the translation phase

etc...

Also, I thought that constants are always constant (this is what I learned from school)

Please can someone understand this a bit?

+5
source share
4 answers

There are two almost completely unrelated concepts here:

  • "constant expressions" is code that can be run at compile time.
    • 4+5 is a constant expression.
    • const A=4; makes A constant expression sometimes in some contexsts, since it is const and is initialized from the constant expression 4 . (This applies only to C ++, not to C)
    • B=A; A may be a constant expression, but B is a variable and may not be in constant expressions.
  • const variables are variables that the function (or structure) of the promises does not change, although other things can change the same variable.
+2
source

In C (unlike C ++), an arithmetic expression is a "constant expression" only if each value in the expression is a numeric constant or the name of an enumeration value. That is, although you could declare a variable as static const int , you still cannot use this (constant) variable in a constant arithmetic expression.

Note that “constant expression” is a phrase defined by a formal standard that defines the C language. There are other expressions that are intuitively constant, but they are not included in the formal definition.

A variable with "static storage duration" is simply a variable that exists at run time. Most of these variables are global variables (i.e. they are not part of any function, not even main ), but in C and C ++ you can have a static variable inside the scope of the function. Such a variable is initialized only once, and only one instance of it exists, regardless of how many times the function is called.

Global variables and other variables with a static storage duration can only be initialized with a constant expression as defined above. This is the case whether they are const variables. The problem is that the variables have a static storage duration, which means that they must be initialized before the program runs. (During program execution, there is a variable with a static storage duration, therefore, if it is initialized, that is, an initial value is specified, and a value is not assigned during program execution, initialization must be performed before the program executes.)

In C ++, a variable declared static const is considered a constant value, so it can appear in constant expressions. In C, however, this is not the case, so the C compiler does not need to track the initial value of the static const variables.

+2
source

you are confused by const and constant .

const decorator decorates the variable and should have a memory cell (but not necessarily true with register .), it basically shows the person and the compiler that the value of this variable should not change.

constant is an expression that the compiler knows about the value.

so if you do:

 const float PI = 3.14; 

in the file area, it will allocate memory and have a static storage duration , which essentially has the same lifetime as the process.

but if you do:

 #define PI2 (3.14) 

this is a different story, since no memory will contain this information.

so if you write:

 float foo1 = 2 * PI; float foo2 = 2 * PI2; 

you can be sure that foo2 will be directly assigned 6.28 after compilation, and whether foo1 such or not is determined by the implementation, since it really depends on an optimization skill called constant substitution.

+1
source

In C, a const -qualified variable is not the same as a constant. The integer literals 50 and 100 are constant expressions; their values ​​are known at compile time (that is, they can be evaluated during the translation phase, which means when the compiler translates the source code into machine code). However, the variables a , c and d will not have their values ​​set up to run time 1 ; this means that they cannot be used in a context where a compilation time constant of 2 is required (they cannot be estimated during the translation phase). The entire const qualifier indicates that the compiler rejects any code that tries to change these variables after they are initialized.

The problem with the related question is that endX and endY are declared in the file area outside of any function. Because of this, the variables have a static storage duration, that is, the memory for them is saved when the program is first loaded into memory, until main is executed and held until the program finishes 3 . Since they are loaded before main executed, they cannot be initialized with an expression whose value will not be known until main executed.


1. I know that at least one version of gcc will create an executable file so that any static const -qualified variables will have their initial value set when the program loads; however, they are still processed as if they were not initialized until main begins.

2. In this respect, C ++ is different; static const -qualified variables are considered compile-time constants in this language.

3. Variables declared in a function or block with the static also have a static storage duration, that is, they exist for the program lifetime, but are not accessible outside this function or block.
+1
source

Source: https://habr.com/ru/post/1209576/


All Articles