| From: Jamie Lokier <lk@tantalophile.demon.co.uk>
| So writing a constant directly can be more efficient than declareing a
| variable which you never change, even a const variable. C++ has the
| rule that const variables are assumed never to change, so the compiler
| can treat the values as constant from the point where they're defined.
| However, C does not have this rule: you can take the address of the
| variable, cast to a non-const type, and change the value. For C, the
| compiler must use the changed value. (At least, that was the rule at
| one time; maybe it has changed now with C0X).
I don't think that this is the case. If you define a variable to be
const, it must not be changed by the program. If, however, you have a
pointer-to-const, it is possible that the object pointed to may change
due to other parts of the program having non-const access.
In other words, a pointer-to-const can point to something that isn't
const. But something that is const must not be changed.
So, in the quoted example, gcc could assume (and even enforce) that
the variable isn't changed during its lifetime.
This is why the compiler is free to place const objects with static
storage duration in read-only memory. For objects of automatic
storage duration, systems usually don't have a suitable mechanism to
prevent wayward modifications.
Hugh Redelmeier
hugh@mimosa.com voice: +1 416 482-8253
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/
This archive was generated by hypermail 2b29 : Tue Feb 29 2000 - 21:00:18 EST