From: Greg P. <gp...@ap...> - 2013-02-04 18:32:36
|
On 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. C99 fixed this. Type punning through a union is now officially supported. (The result depends on the implementation-defined bit representation of the objects of course, but I expect valgrind already requires the implementation to be two's-complement integers and IEEE float.) valgrind probably performs formally-undefined aliasing and type punning everywhere anyway, which is why it builds with -fno-strict-aliasing. -- Greg Parker gp...@ap... Runtime Wrangler |