www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/19/01:03:19

From: Jack Klein <jackklein AT spamcop DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [OT] About const int
Message-ID: <3dpcltkce8jgot617ngpt49r16aoqj1evn@4ax.com>
References: <5BF60CD649EDD411A04600B0D049F53AFFA913 AT hydmail02 DOT hyd DOT wilco-int DOT com> <9j4g6a$qel$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
Lines: 38
Date: Thu, 19 Jul 2001 04:45:50 GMT
NNTP-Posting-Host: 12.84.12.123
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc04-news.ops.worldnet.att.net 995517950 12.84.12.123 (Thu, 19 Jul 2001 04:45:50 GMT)
NNTP-Posting-Date: Thu, 19 Jul 2001 04:45:50 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019