Reputation: 11901
I want to know about const
internals in C and C++ . How does the compiler impose "constantness" ?
Upvotes: 14
Views: 5583
Reputation: 320391
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.
Upvotes: 12
Reputation: 12382
This is actually a very complicated thing in an optimizing compiler. It starts out simple, though.
When you declare a variable with the const keyword (let's just ignore pointers because they can be const or point to const or both) the compiler remembers that no code should be changing that variable (almost). If the compiler sees code that changes a const variable then it considers that an error (or occasionally only worthy of a warning, but for simplicity sake I'll ignore it). The compiler also assumes that no code that it cannot see (right now anyway {code in other .c files or possibly library or .s or .asm files) will change the const variable (unless it is const volatile
in which case it will assume that it could change at any moment, but will still enforce not letting your code change it -- this is useful for memory mapped SFR [special function registers] that are used to read a device's state, but can't be written to. Remember that c and c++ are used for OS and embedded programming).
The assumption that a variable will not change under some or all circumstances allows the compiler's optimization routines to do things that it otherwise wouldn't be able to do. This means stuff like placing the literal value of a variable into the instruction stream rather than loading the variable's address and then loading the variable's value. It can also assume that if it loaded that if:
extern const int foo; // note that the value isn't visible, so a load is necessary
...
extern int baz(int, int);
...
int bar(int x, int y) {
int m, n;
int r = x / foo; // this would require loading the value of foo from RAM
m = baz(r, y); // the compiler normally has to assume that a function could change a global
n = m + r + foo; // but since the global foo is const it shouldn't be able to be changed
// by the call to baz and does not need to be reloaded
return n;
}
An executable file (or other object file) may have a section (.rodata) that has only constants in it. In many cases the OS may enforce not allowing a program to write to this data (or it may even be in ROM in some cases). This section may also contain versions of non-const variables which are used for initialization, since it may have any constant in it, not just constants declared as const.
So in C const mainly just tells the compiler to tell you that you've screwed up and are trying to change something that should not have been changed. It is allowed to make some assumptions based on it, though.
In C++ it gets more complicated. I don't remember all of the details, but I do recall that you can overload a function name based on if a value you pass to it is const or not, which can be useful.
Upvotes: 1
Reputation: 3481
In addition to the compile-time enforced immutability provided by using the const
keyword that other answers to your question have already mentioned, using it sometimes allows the compiler to place such data in the read-only section of a binary and memory. According to Section 2.4.2 "Forever const
" in Ulrich Drepper's article How To Write Shared Libraries doing so potentially allows programs to (1) use less resources and (2) start up faster.
Note that casting away the const
ness of data in such read-only areas of memory results in the undefined behavior, as usual.
Upvotes: 5
Reputation: 19304
In C, the const keyword will make a variable immutable, meaning that it cannot be modified.
Usually, this is a compile-time distinction and has no effect on runtime modification of a variable. For instance, a const variable can be indirectly modified using a pointer to the same memory address.
In C++, the const keyword takes on more than one meaning.
For instance, a class member function can be "const", meaning that it is not allowed to modify the state of a class instance.
As in C, a variable declared const can also be modified indirectly using a pointer, but also by using the "mutable" keyword or the const_cast<> operator.
Upvotes: 1
Reputation: 29090
When the compiler is compiling the code, it computes the type of each expression so it can type-check them and emit code correctly (such as warning when you try to store an int in a pointer, or properly converting an integer to a double). const
-ness can be considered part of the type. Since it has the type information for the expression, it can check the type of the lvalue (left-hand side of the assignment) and throw an error if it has 'const' in its type.
Upvotes: 2
Reputation: 100013
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.
Upvotes: 28