3 ms·
The use of union here is bit confusing (I think its unnecessary?), although I don't imagine it making any difference in the generated code.
by zokier 3y ago
The use of union here is bit confusing (I think its unnecessary?), although I don't imagine it making any difference in the generated code.
- mwkaufma 3y agoThat'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.
- progmetaldev 3y agoAgreed, as someone that isn't very familiar with C, but has used many C-like languages, the names "as_str" and "as_int" threw me off.