🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4276

Re: Aliasing in C

#4276
From: Thomas Wolf
Date: Thu, 26 Oct 1995 09:09
48 lines
2116 bytes
In article <46msr4$ee9@elaine25.Stanford.EDU>, sanjoy@leland.Stanford.EDU (sanjoy ghosh) writes:
:> Hi,
:> 
:> I was looking at the code generated for the following program:
:> 
:> struct try {
:>         int     tr1;
:>         int     tr2;
:>         int     tr3;
:>         int     tr4;
:> };
:> 
:> int try1 (struct try *array, int *index)
:> {
:>         int     i;
:> 
:>         for (i = 0; i < 10; i++)
:>         {
:>                 array[index[i]].tr3 = i;
:>                 array[index[i]].tr2 = i + 10;
:>                 array[index[i]].tr1 = i + 20;
:>         }
:> }
:> 
:> 
:> Most compilers assume that index and array could be aliased and reload index[i]
:> for each of the 3 indexes into array in the loop body.  So they load index[i]
:> and compute the address of array[index[i]] 3 times.  Is this really necessary
:> in ANSI C?  Can't we assume that index and array cannot overlap since they
:> point to non-compatible objects, and if you write to one you can screw up the
:> other?  Do any compilers assume that they are NOT aliased and optimize the
:> indexing?
:> 
:> Xlc has a -qansialias option but that doesn't seem to help.
:> 

But 'index' might point to one of the fields within the struct 'try', so it
might be modified by the assignments within the loop.

Regards,

  Thomas
----------------------------------------------------------------------
Swiss Federal Institute of Technology | Thomas Wolf
Software Engineering Laboratory       | EPFL-DI-LGL
Thomas Wolf (TW)                      | CH-1015 Lausanne (Suisse)
E-Mail: wolf@di.epfl.ch               | Phone: (++41 21)693 42 37
---------------------------------------------------------------------- 
Technical Details
Message-ID: <1995Oct26.100732@lglsun.epfl.ch>
Path: arc1-ca.pugleaf.net!swidir.switch.ch!epflnews!lglsun!wolf
References: <46msr4$ee9@elaine25.Stanford.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!swidir.switch.ch!epflnews!lglsun!wolf
From: wolf@lglsun.epfl.ch (Thomas Wolf)
Newsgroups: comp.std.c
Subject: Re: Aliasing in C
Date: 26 Oct 1995 09:09:42 GMT
Organization: Ecole Polytechnique Federale de Lausanne
Distribution: world
Message-ID: <1995Oct26.100732@lglsun.epfl.ch>
References: <46msr4$ee9@elaine25.Stanford.EDU>
NNTP-Posting-Host: lglsun2.epfl.ch