[PATCH] fix aliasing for chars in global structures - take II
Daniel Jacobowitz
drow@mvista.com
Fri Mar 22 11:42:00 GMT 2002
On Sat, Mar 23, 2002 at 06:21:55AM +1100, Fergus Henderson wrote:
> On 22-Mar-2002, Dan Nicolaescu <dann@godzilla.ICS.UCI.EDU> wrote:
> > Fergus Henderson <fjh@cs.mu.oz.au> writes:
> > > Consider the following program:
> ...
> > > Isn't that program strictly conforming C, which should output `4'?
> > > And won't your patch mis-optimize it to output something
> > > different?
> >
> > I don't think so. The the references to ps1->f1 and ps2->f2 will be in
> > the same alias set.
>
> Sorry, I got the example wrong.
> Let's make it
>
> struct first { unsigned char f1; int x; };
> struct second { unsigned int f2; double y; };
>
> so that they are not in the same alias set,
> and change the code to something like this:
>
> void
> g (struct first *ps1, struct second *ps2)
> {
> ps1->f1++;
> ps2->f2 = 0;
> ps1->f1++;
> }
>
> int
> main (void)
> {
> void *p = calloc (sizeof(struct first) + sizeof(struct second), 1);
> g (p, p);
> printf("%d\n", ((struct first *) p)->f1);
> return 0;
> }
>
> This one should print "1", but I suspect that with your patch it will
> print either "0" or "2".
At this point you're well into illegal C, I think. Why do you believe
you can pass the same memory area as a `struct first' and a `struct
second', and legally access them, when they do not share a common
prefix?
--
Daniel Jacobowitz Carnegie Mellon University
MontaVista Software Debian GNU/Linux Developer
More information about the Gcc-patches
mailing list