[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs

Uoti Urpala uoti.urpala
Mon Feb 1 00:31:55 CET 2010

On Sun, 2010-01-31 at 15:56 -0500, Alexander Strange wrote:
> It's not legal to cast a pointer like that, so any type cast without extra attributes can't work with strict aliasing:
> C99 p7:
> A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is
> converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.

There's the alignment issue that I mentioned in my previous mail, but if
the address happens to be suitably aligned then nothing here should
prevent the cast.

> There's no text about dereferencing a converted pointer, so it's undefined.

The above paragraph talks about the conversions only and doesn't seem to
be relevant to dereferencing. I looked at the thread you linked below
and there some expressed claims that this would limit dereferencing, but
I find that to be a somewhat weird interpretation. At least that would
IMO be somewhat impractical.

> It's also forbidden in the gcc manual:
> http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Type_002dpunning
> There's a long recent gcc thread about this here:
> http://thread.gmane.org/gmane.comp.gcc.devel/111111
> Casts with may_alias were found to work for x264, but the portable solution is probably either no strict aliasing or using memcpy().

Why would no strict aliasing be more portable than may_alias?

More information about the ffmpeg-devel mailing list