|
From: Eliot M. <mo...@cs...> - 2013-02-04 20:11:28
|
On 2/4/2013 12:45 PM, Tom Hughes 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. Of course it is sometimes useful. For example, if you wish to serial and IEEE floating point number into a file or over a pipe or network connection, you need to reduce it to bytes and to control the order. From the standpoint of general C, it is "undefined" since they cannot give a definition apart from the machine (and apart from any given compiler's rules about how it lays out struct fields, etc.), but I agree that "implementation defined" is a more useful way to think of it. -- Eliot Moss |