3 ms·
That's the accepted method in c to do well-defined type-punning.
by mwkaufma 3y ago
That's the accepted method in c to do well-defined type-punning.
- vinkelhake 3y agoBut the `as_str` field is never used. The type punning (and all the wonderful endian issues) is already happening in the memcpy.
- mwkaufma 3y agoOh yeah, wheee, I'm not carefully reading. Maybe it's vestigial leftovers from a port from c to c++ (major impls handle it in C++ like in C but the standard insists it's UB).
- mike_hock 3y agoMight be leftover from doing type punning through the union, then being told that's UB and using memcpy instead.
- saagarjha 3y agoIt’s one accepted method. Typically it is recommended to use memcpy.
- JonChesterfield 3y agoIs that still the case these days? I thought we lost that ability sometime around C99 (it's now UB to read from the inactive member), and you're now supposed to memcpy all over the place and hope the compiler elides it.
- Denvercoder9 3y agoThat's only in C++, type punning through unions is still legal in C.