www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/20/06:15:05

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [OT] About const int
Date: 20 Jul 2001 10:13:26 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 36
Message-ID: <9j9086$ogr$1@nets3.rz.RWTH-Aachen.DE>
References: <20010720002232 DOT 42201 DOT qmail AT web9906 DOT mail DOT yahoo DOT com>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 995624006 25115 137.226.32.75 (20 Jul 2001 10:13:26 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 20 Jul 2001 10:13:26 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Lets Go Canes <letsgonhlcanes AT yahoo DOT com> wrote:
> Hi all.

> What you are doing is called "aliasing".  Many compilers would give you
> the same result even if you removed the "const" qualifier.  

No. At least not any correct compilers conforming to Standard C in its
traditional version dated 1989/1990, and I strongly believe those to
be the overwhelming majority of compilers in use these days.  Standard
C requires that all such side effects, whether by pointer aliasing or
other means, be respected correctly, to a certain extent: no two such
side effects are allowed to affect the same object between any two
"sequence points".  A compiler that doesn't account for such side
effects is broken beyond recognition. The program at hand does have
undefined behaviour, but not because of aliasing.

C++ is no different from C, in this respect, to the best of my
knowledge.

In fact, this aliasing issue is one of the reasons C programs can
sometimes not be optimized as perfectly as some programmers want them
to be -- it's one of the main reasons some people still insist coding
in language like FORTRAN.  That's why, in the new C99 standard, a
keyword was introduced that allows the programmer to state explicitly
that a particular variable is not aliassed by any pointer: "restrict".

> Basically, the compiler doesn't know that "k" is a pointer to the
> same memory occupied by "j" (i.e., "k*" is an alias for "j"), and so
> if it already has the value of "j" in a register, it will use it,
> even though you just changed the value of "j" via the "k*" alias.

If it did that, it'd be a severly broken compiler.

-- 
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


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