Background knowledge
Before you start, say something about the symbol table, the compiler. The following look at the Baidu Encyclopedia Description:
A symbol table is a data structure used in a language translator. In the symbol table, each identifier in the program source code is bound to its declaration or usage information, such as its data type, scope, and memory address.
The symbol table collects, records and uses information about the types and characteristics of some grammatical symbols in the source program during the process of compiling the program. This information is generally stored in a tabular format in the system. such as constant table, variable name table, array name table, procedure name table, label table and so on, collectively referred to as the symbol table. For the symbol table organization, construction and management methods will directly affect the efficiency of the compilation system.
There is another problem: the front said seems to be annoying, since the declaration into a const, why still old modification ah?
According to the C + + standard, for modifying a const variable, it belongs to: undefined behavior (refers to the behavior of unpredictable computer code), so this behavior depends on the specific implementation of the various compilers (that is, different compilers may behave differently).
See, the C + + standard is not advocating such a play.
Let's take a look at the previous control procedure:
intMain () {intI0 = One; Const inti =0;//Defining Constants I int*j = (int*) &i;//See here can take the value of I, Judge I must after their own memory space*j =1;//Modify the memory that J points toprintf"0x%p\n0x%p\n%d\n%d\n", &i,j,i,*j);//Watch the experimental results Const intCK =9;//This control experiment is to observe that the effect of a reference to a constant CK intIK =ck; intI1 =5;//This control experiment is to distinguish between constants and references to variables. intI2 =I1; return 0;}
Also change the statement of I to:
Const volatile int 0; // Defining Constants I
We know that the volatile is to tell the compiler in the process of translating the source code into assembly language (machine code), do not optimize. The source of the access to I, translated into memory every time to fetch data (here is to do the compilation work, just to translate the corresponding source code into assembly language), rather than from the symbol table (constant table) crawl data. So with the volatile keyword I's access is to go in memory to take the data, do not go to the constant table.
Now there is a problem, if I add the volatile keyword, then every time I access statements are translated into the "to see the memory data" This behavior of the assembly language, there will be no constant replacement process, then the following statement is not legal?
ten;
You changed the value of the constant, how can it be legal? But according to the above statement for the const volatile int type I seems to be legal again. Here's the problem:
Part of the compiler workflow is this: syntax detection-precompiled (macro substitution, constant substitution, * (&i) constant equals I, etc.) compile. So i = 10 this sentence in the grammar check this stage, see you to a Const constant assignment, has been an error, can not compile this stage.
But some compilers seem to ignore the volatile keyword and look at the results of the test:
(1) Look at the results of vc6.0:
Without the volatile keyword:
With the volatile keyword:
That is, in the vc++6.0 compilation environment, the volatile modifier is added when the const variable is defined, the same as not adding the effect. The compiler has taken optimizations (even turning the compiler optimization option off or so, helpless).
(2) The test results of VS2010 and g++ are the same:
No volatile keyword:
There are volatile keywords:
Therefore: It is not recommended to modify the value of a const variable, even if the modification is familiar with how the currently used compiler interprets the undefined behavior .
C + + constant folding--const and volatile