Dave Vandervies said:
Thanks a lot!
It means that casting-out const is not as bad as I thought it was,
and the promise is rather a weak one - more like a wishful thinking!
I thought that the compiler may locally assume that after taking an
address of an object into a pointer to const type version, the object
won't change. I was wrong.
Note that if an _object_ is defined with the const qualifier, it really
does mean "this object can not be changed by the program"; any attempts
to change it (by casting away const from a pointer to it, most likely)
invoke undefined behavior.
This means that you do need to be careful with casting away const, and
make sure that you don't do it unless you know you won't end up trying
to write to a const object through the de-const-ed pointer.
A lot of library functions (f'rexample, the string-searching ones) do
something like this; they take a const pointer (to indicate that they
don't modify the pointed-at data), do the appropriate operation, and
cast away the const qualifier before returning a non-const pointer into
that data. If you give them a const pointer, it's your responsibility
to put the result back into a const pointer so that you don't lose the
constness (but if you're reasonably careful with this then it will do
The Right Thing in either case, since converting a const pointer to a
non-const pointer (with the equivalent of a cast in the library function)
and then back to a const pointer (by assigning the result in your code)
is valid and well-defined.
("const pointer" in the above paragraph refers to "pointer to const
object" (const foo *ptr), which is not the same thing as declaring the
pointer itself const (foo * const ptr).)
It seems to me that `const' in C is a device to generate warnings or
errors rather than to change semantics of a program. If we removed all
`const' quals from a program, the binary code output should not change,
should it?
Pretty much, yes.
Though I suspect that along with `restrict' in C99 it may actually make
some optimization possible; f'rexample, a pointer argument with both
const and restrict qualifiers can be assumed to not be changed through
any other pointers given to the function (though I don't know whether
this covers, say, access to a global variable from a function called by
the function getting the const+restrict pointer).
[OT] Do you happen to know if `const' has the same semantics in C++?
(There are of course different constraints - C++ is more type sensitive,
different conversion rules etc. I'm only asking in context of what has
been said above.)
It doesn't; C++ has const semantics that (combined with its general
greater type sensitivity) are actually strong enough to be useful.
F'rexample, combined with being able to overload functions on different
types, this allows C++ to handle the aforementioned case of library
functions casting away const on pointers they're given by returning a
pointer with the same constness of the pointer they're given, so giving
it a const pointer and assigning the result to a non-const pointer
(having the same effect as casting away const, but without a visible
cast in your code) becomes an error the compiler is required to catch.
dave