Mail Archives: djgpp/2001/07/19/01:03:19
On 18 Jul 2001 17:14:50 GMT, Hans-Bernhard Broeker
<broeker AT physik DOT rwth-aachen DOT de> wrote in comp.os.msdos.djgpp:
> Prashant Ramachandra <rprash AT wilco-int DOT com> wrote:
> > I have a question that's probably got nothing to with DJGPP, so please
> > forgive me for this off-topic post.
>
> > I tried the program below and interestingly, it gives me...
>
> > "Constant is 10. It is actually 1"
>
> That program is very incorrect. It causes undefined behaviour at the
> point of the cast from (const int *) to (int *). Gcc would have warned
> you about this, if only you had asked for assistance (rule of thumb:
> always use '-Wall' unless you have very good reasons not to).
It does not cause undefined behavior at the point of the cast to
pointer to non-const int. It would not cause undefined behavior if
that pointer were used to access the value of the const int, that is
read it. It is only undefined behavior when the code attempts in any
way to actually modify the value of the object or any of its bytes.
> > Shouldn't gcc not make the assumption that the value of j *is* 10 and
> > instead reference itfrom memory instead.
>
> No, it shouldn't. You'll have to make up your mind: either j *is*
> constant, or it isn't. You can't have it both ways.
>
> If you lie to the compiler like in this piece of source code, _you_
> take responsibility for all consequences.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
- Raw text -