|
From: Patrick J. L. <lop...@gm...> - 2013-02-04 18:03:21
|
Well, this appears to be a bit complicated. According to the GCC manual, using unions for "type punning" is permitted, provided the underlying type really is a union: <http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fstrict_002daliasing-879> On the other hand, a little searching on StackOverflow suggests that this is technically still undefined behavior. So a union will work on GCC, which probably means it will work on Clang and the Intel compiler... But "by the book", it's still no good. Whether this is good enough for Valgrind is up to you :-) - Pat On Mon, Feb 4, 2013 at 9:45 AM, Tom Hughes <to...@co...> wrote: > On 04/02/13 17:25, Patrick J. LoPresti wrote: > >> Never access an object via the wrong pointer type. The only exception >> is "char *", which can alias anything by definition. (You can also >> use a union to access the same memory as different types. I believe >> this takes you into the realm of implementation-defined behavior. >> That is also bad, in my opinion, but it is not the disaster of >> undefined behavior.) > > > The C standard says you can only read a union through the member it was last > written through. My guess would be that doing otherwise is invokes undefined > behaviour, but you could be right that is is "just" implementation defined. > > Tom > > -- > Tom Hughes (to...@co...) > http://compton.nu/ |