The following is a reference to some information on the Internet and verified by the program.
Note that the following conditions are the result of the GCC and g++ compilers, which are different with the VS compiler.
Here are some differences between the constants defined by const in C and C + +:
When a const is defined in C + +, it is not allocated a space to it, but instead it is written to the symbol table, which makes it a constant during compilation, without the operation of memory and read, making it highly efficient. But a const-defined constant is essentially a variable, a variable has an address, and when does it allocate memory? Take a look at the following code:
int Main () { constint a 2; int* p = (int*) (&a); - ; cout<<&a<<Endl; cout<<p<<Endl; cout<<a<<Endl; cout<<*p<<Endl;}
Results:
We see that through int*p = (int*) (&a), this method can directly modify the value in the memory space corresponding to the const constant, but this modification does not affect the value of the constant itself, because when a is used, the compiler does not go to read the memory space at all. This is the constant folding of C + + (constant folding), which places the const constant in the symbol table without allocating memory to it. The compiler directly replaces the optimizations. Unless a storage space is needed, the compiler has to allocate a space to a, but then the value of a is still read from the symbol table, no matter how the value in the storage space of a changes, there is no effect on constant a.
But that's not true in C. C without the concept of constant folding, when defining a constant with constant, the compiler will directly open up a memory space to hold the constant. No optimizations are made. The same example will produce different results under C:
1 intMain ()2 {3 Const intA =2;4 int* p = (int*) (&a);5*p = -;6printf"%x\n",&a);7printf"%x\n", p);8printf"%i\n", a);9printf"%i\n",*p);Ten return 0; One}
Results:
As we can see, in C, a value that is defined by const as a constant is officially modified, and the compiler does not report any errors!
If we go further, we can find that for the above two examples, a is defined within a function (such as the main () function), whether C or C + +, essentially just treats it as a normal local variable, just allocates space on the stack. Therefore, Const cannot play a role in preventing changes to its memory space, and a valid coercion type conversion can be done easily. C + + is better than C is the use of constant folding mechanism, so that the value of the constant is independent of the corresponding memory space, thereby protecting the constant value.
The above example is for the local const constant, for the global const variable, C + + still uses constant folding strategy, so the following code is OK:
// global variable Const int 3 ; int Arr[a];
But C will error: error:variably modified ' arr ' at file scope because GCC thinks a is just an ordinary global variable, and the variable cannot be used to specify the length of the array. Of course, this is for the global array, if it is a local array, even if it is an int a = 3; int Arr[a]; This is OK, because C there is also a kind of thing called variable-length array (I faint ~, seemingly because the implementation mechanism of the two are different, this should look again)
In addition, for a, in C and C + + if we still use int *p = (int*) (&a), this method modifies its memory value, compiles without error, but the runtime reports a segment error because a is placed in the read-only global data area, and modifying the data in that area throws a segment error.
Under the VS Compiler:
1. Variable-length arrays are not supported, and a variable cannot be used to declare the length of an array unless it is declared as Const.
2.const variables, whether global or local, are placed in the read-only data area, so you cannot use the previous method to modify the value inside the memory space, compile error.
Some differences between C and C + + about const