Article View: comp.std.c
Article #4275Aliasing in C
From: sanjoy ghosh
Date: Thu, 26 Oct 1995 02:47
Date: Thu, 26 Oct 1995 02:47
36 lines
1321 bytes
1321 bytes
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.
Thanks,
Sanjoy
Technical Details
Message-ID:
<46msr4$ee9@elaine25.Stanford.EDU>Path:
arc1-ca.pugleaf.net!news.Stanford.EDU!not-for-mailShow 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: Aliasing in C Date: 25 Oct 1995 19:47:00 -0700 Organization: Stanford University, CA 94305, USA Message-ID: <46msr4$ee9@elaine25.Stanford.EDU> NNTP-Posting-Host: elaine25.stanford.edu