https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100224

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Zhao Chun from comment #2)
> (In reply to Richard Biener from comment #1)
> > You are accessing 'double' via a pointer to uint64_t * here:
> > 
> >         k = *((uint64_t*)data);
> > 
> > that violates type based aliasing rules.  You can use -fno-strict-aliasing
> > to work around your bug or use
> > 
> >     typedef uint64_t aliasing_uint64_t __attribute__((may_alias));
> >     k = *((aliasing_uint64_t*)data);
> 
> Thanks for your answer, it works for me.
> 
> However I don't quite understand why it works after such a change. Could you
> please explain it more clearly?

Using a may_alias attributed type tells GCC to treat it like a 'character type'
in terms of what the C/C++ standards allow.  Note that this is not portable.
A solution that works with all compilers I know is doing

   memcpy (&k, data, sizeof (uint64_t));

Reply via email to