This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Testcase for strict-aliasing wanted


> Date: Fri, 18 Jan 2002 21:39:38 +0200 (EET)
> From: Osku Salerma <osku@iki.fi>
> To: gcc@gcc.gnu.org

> Does anybody have a simple testcase available that breaks with strict
> aliasing enabled?

Ok, here is a quick quiz to determine if you know C.  Does the
following program always print:

a)	  12345678	 or	12345678	(depending on endian)
	  12340000		5678

or

b)	12345678
	12345678

or c) neither.

?  Answer at the end.

#include <stdio.h>
 
int
main ()
{
    int a = 0x12345678;
    unsigned short *b = (unsigned short *)&a;
 
    printf ("%x\n", a);
    b[1] = 0;
    printf ("%x\n", a);
 
    return 0;
}

If you said a, you'd be wrong, if you said b, you'd be wrong.  This is
non-portable in ANSI/ISO C.  With the older compilers, those without
type based aliasing, one would always get a, with newer compilers with
more powerful optimizers, it is more common to get b if one optimizes.

[ ... ]


Isn't this in the C programming FAQ yet?  If not, please go over there
and get it added.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]