Article View: comp.std.c
Article #4286Re: Aliasing in C
From: sanjoy ghosh
Date: Fri, 27 Oct 1995 16:35
Date: Fri, 27 Oct 1995 16:35
54 lines
2565 bytes
2565 bytes
In article <DH3Mtn.ECM@ukpsshp1.serigate.philips.nl>,
Stephen Baynes <baynes@ukpsshp1.serigate.philips.nl> wrote:
>Thomas Wolf (wolf@lglsun.epfl.ch) wrote:
>: 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]
><snip>
>: But 'index' might point to one of the fields within the struct 'try', so it
>: might be modified by the assignments within the loop.
>
>But a very clever compiler (don't ask me to write it) could spot that index[0]
>through index[10] is used so 'index' cannot legitimatly point to one of the
>fields in struct 'try'. If it did then you would have undefined behaviour.
>Another posibility is that 'index' and 'array' are different members within
>the same union - but that still means you have undefined behaviour.
>I can't think of any case where they can be aliasis and not trip over
>undefined behaviour.
Yes, you are right. In fact I was thinking of this line in the Standard,
6.3.16.1 If the value being stored in an object is accessed from another
object that overlaps in any way the storage of the first object, then the
overlap shall be exact and the two objects shall have qualified or
unqualified versions of a compatible type; otherwise, the behaviour is
undefined.
Does the above apply to this example? And what exactly does it mean?
Thanks,
Sanjoy
Technical Details
Message-ID:
<46r1pc$gcd@elaine25.Stanford.EDU>Path:
arc1-ca.pugleaf.net!news.Stanford.EDU!not-for-mailReferences:
<46msr4$ee9@elaine25.Stanford.EDU> <1995Oct26.100732@lglsun.epfl.ch> <DH3Mtn.ECM@ukpsshp1.serigate.philips.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.Stanford.EDU!not-for-mail From: sanjoy@leland.Stanford.EDU (sanjoy ghosh) Newsgroups: comp.std.c Subject: Re: Aliasing in C Date: 27 Oct 1995 09:35:56 -0700 Organization: Stanford University, CA 94305, USA Message-ID: <46r1pc$gcd@elaine25.Stanford.EDU> References: <46msr4$ee9@elaine25.Stanford.EDU> <1995Oct26.100732@lglsun.epfl.ch> <DH3Mtn.ECM@ukpsshp1.serigate.philips.nl> NNTP-Posting-Host: elaine25.stanford.edu