This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Question on strict aliasing in C.
- From: Andrew Haley <aph at redhat dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 04 Jul 2011 18:07:42 +0100
- Subject: Re: Question on strict aliasing in C.
- References: <4E11F0FC.7020601@gjlay.de>
On 04/07/11 17:57, Georg-Johann Lay wrote:
> Hi,
>
> this it not a question on GCC, so I apologize for asking a question on
> C strict aliasing rules here. As I know that some people reading this
> list are much more familiar with C standard than I am, allow me to ask
> that question, anyway.
>
> Suppose the following C code that tries to implement the standard
> copysign function, i.e. copy the sign of y into x and return that
> value. double be 64 bits wide:
>
>
> #define MASK ((unsigned short) 0x8000)
>
> double copysign (double x, double y)
> {
> unsigned short * const px = (unsigned short*)(char*)&x + 3;
> unsigned short * const py = (unsigned short*)(char*)&y + 3;
>
> *px = *px & ~MASK | *py & MASK;
> return x;
> }
>
>
> While I say that this code is not correct because it breaks C's strict
> aliasing rules (e.g C89/90, Chapter 6.3; C98/99, Chapter 6.5, Clause
> 7), some other person very well familiar with the standard claims that
> is correct and no problem.
>
> So I want to reassure me if the code is ok or not.
It's not. Tey're wrong, you're right.
Hope this helps. :-)
Andrew.