In depth analysis of the implementation mechanism of const in C++

  • 2020-04-01 21:30:02
  • OfStack

The problem
What does const mean in C and C++? How is the specific implementation mechanism realized? This article will do some analysis on these two issues, briefly explaining the meaning of const and the implementation mechanism.

Problem analysis
Simply put, const represents read-only variables in C and constants in C++. There will be time for another post on the use of const in C and C++ and more differences.

So how exactly is const implemented? How does the compiler implement const for built-in types declared as const, such as int, short, long, and so on? So are non-built-in types treated in the same way as built-in data types, such as struct types? Here are some small examples to illustrate these problems:
C const example :
 
const int i=10; 
int *p=(int *)(&i); 
*p=20; 
printf("i=%d *p=%d n",i,*p); 

Guess what the output is? I = 20 * p = 20
C++ language const example 1:
 
const int i=10; 
int *p=const_cast<int *>(&i); 
*p=20; 

cout<<"i="<<i<<" *p="<<*p<<endl; 

The output is I =10 *p=20
C++ language const example 2:
 
struct test{ 
int j; 
char tmp; 
test() 
{ 
j=30; 
tmp='a'; 
} 
}; 
int main(int argc, char* argv[]) 
{ 
const struct test t1; 
int *q=(int *)(&t1.j); 
*q=40; 
cout<<"j="<<t1.j<<" *q="<<*q<<endl; 
return 0; 
} 

The output is j=40 *q=40

Sample result analysis
See the above three sets of output results, do you feel very strange:
Problem 1. For the variable I of type const int, after the value is modified by pointer p in C language, I becomes 20; In C++, I is still 10 after changing the value by pointer p.
Question 2. The element j of const struct test in C++ is changed by pointer q. Why is the reaction mechanism of const int different from that of const struct test?

For question 1, we know that in the C language const said read-only variables, since the const as a variable, so it will be stored in the memory of his space, and can change the memory space by indirect pointer value, after changing the values in the memory by pointer p, to obtain the value of I, will visit the space, get the value was changed. C++ treats const as a constant, and the compiler simply replaces references to I with constants, such as cout< < I; It would be understood as cout< < 10; Instead of accessing the memory address of I to fetch the data, it's kind of like the macro #define I 10 in C. So in C++ I will print 10, and *p will print 20.

For problem 2, the C++ language only makes constant substitution for built-in data types, but not for non-built-in data types such as structs. Since the struct type is not a built-in data type, the compiler does not know how to directly replace it, so it must access memory to fetch data, and accessing memory to fetch data will inevitably fetch the value changed by pointer q, thus resulting in a completely different processing mode from that of const int in C++.

summary
There are many differences and connections between C language and C++, and const is only one of them. Understanding and using const is the basis of programming, which requires proficiency.

Related articles: