On Tue, Jun 23, 2020 at 4:29 AM Henri Menke <[email protected]> wrote:
> > > No, type punning *is* undefined behaviour. > > 6.2.6.1 General > > 7 When a value is stored in a member of an object of union type, the > bytes of the object representation that do not correspond to that > member > but do correspond to other members take unspecified values. > > http://port70.net/~nsz/c/c11/n1570.html#6.2.6.1p7 hm, unspecified value, not undefined behaviour. Probably this one is ok: \directlua{ ffi.cdef [[ unsigned int c ; union endianess { uint32_t i; uint8_t c[sizeof(uint32_t)]; } ]] local x = ffi.new('union endianess x',{i=0xAABBCCDD}) if ffi.sizeof('uint8_t')==1 and ffi.sizeof('uint32_t')==4 then if(x.c[0]==0xDD and x.c[1]==0xCC and x.c[2]==0xBB and x.c[3]==0xAA) then print "little endian" elseif (x.c[0]==0xAA and x.c[1]==0xBB and x.c[2]==0xCC and x.c[3]==0xDD) then print "big endian" else print "mixed endian" end else print "unable to check endianess" end } \end see eg Modern-C on unions https://modernc.gforge.inria.fr/#orgd2172c0 -- luigi
