This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: cast float * to unsigned long *
- From: Andrew Pinski <pinskia at physics dot uc dot edu>
- To: Susukita Ryutaro <susukita at postman dot riken dot go dot jp>
- Cc: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 16 Oct 2002 23:46:56 -0700
- Subject: Re: cast float * to unsigned long *
On Wednesday, Oct 16, 2002, at 23:36 US/Pacific, Susukita Ryutaro wrote:
I compiled the following code cast.c with gcc 3.2 on Redhat 8.0 / i386.
#include <stdio.h>
int main(){
float h = 0;
printf("%lx\n", *(unsigned long *)&h);
printf("%lx\n", *(unsigned long *)&h);
}
But the output is different between optimization options.
$ gcc -O cast.c
$ ./a.out
0
0
$ gcc -O2 cast.c
$ ./a.out
4212a2d0
0
Is this a bug or a behavior allowed by ANSI?
The behavior is allowed by ISO-C (also ANSI-C) is called aliasing rules.
Use an union instead to get the required effect all the time.
Thanks,
Andrew Pinski