Re: Question on strict aliasing in C.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux