I want to know about const internals in c and c++ . How compiler imposes constantness ? Can some one help me please.
A constant is a value or variable that can't be changed in the program, for example: 10, 20, 'a', 3.4, "c programming" etc.
Yes, there's a const keyword in C. It's been there since C90. Syntactically, it can occur in the same places as in C++.
We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value like the value of PI, you wouldn't like any part of the program to modify that value.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
In general const
is 100% compiler. When you declare something const, the compiler places restrictions on what it will let you write. It won't let you assign to const scalar, assign through a const reference or or pointer, or invoke a non-const function of const object.
There is no guarantee that the compiler will arrange any sort of runtime protection.
const
keyword in C and C++ has two different semantic meanings.
(1) It can declare the constness of an object
const SomeType t;
In the above case object t
is a non-modifiable object. The compiler will do its best to prevent you from modifying it by observing the const-correctness rules (which are not the same in C and C++). The const-correctness rules are enforced only conceptually, at the language level, which means that there are ways to circumvent these rules, and which also means that constness of an object will not be necessarily implemented at physical level. I.e. there's no guarantee that the object will be ultimately placed in read-only memory.
It is important to note that this kind of constness is not removable in a sense that any attempts to modify the above object by casting away the constness lead to undefined behavior (excluding possible mutable
members in C++).
(2) It can declare constness of an access path to an object
const SomeType *p;
The above p
is declared as a pointer-to-const. This does not necessarily mean that the object p
is pointing to is a constant object (as defined by the first kind of const
above). It might easily be a non-constant one, in which case it is perfectly legal to cast away the constness from the above access path and modify the object, although it is generally not a good programming practice. In other words, constness of an access path is potentially removable.
Taking the above into account, the following declaration
const int* const* const* const p = 0;
includes two different kinds of const
: the very last const
declares the constness of the object p
(first kind), while the rest of const
declare the constness of various levels of access path represented by p
(second kind).
P.S. As a [possibly unrelated] side note, it is probably worth noting that the term constant has drastically different meanings in C and C++. In C++ constants are objects declared as const
. In C constants are literals. Objects declared as const
are not constants in C terminology.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With