3 ms·
A related big C myth that I believed for far too long relates to structs / bitfields and how they are NOT required to be packed as tightly as possible by the sp
by mjevans 3y ago
A related big C myth that I believed for far too long relates to structs / bitfields and how they are NOT required to be packed as tightly as possible by the spec.
Worse, offhand, I don't know of any portable / standard way to achieve that. Only compiler specific extensions. (I can hope it might be portable to E.G. clang) https://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4 https://stackoverflow.com/questions/11770451/what-is-the-mea... -- struct __attribute__((packed, aligned(4)))
- jeffbee 3y agoWhich way did the myth go? I am unclear on your statement. In my experience, which admittedly does not include all members of our industry, most people are using `packed` in the same way that Gentoo users were using `-funroll-loops`. In fact, less than 2 hours ago I had to inform my worst colleague (all time career worst) that `aligned(N)` means AT LEAST N, not exactly N. Same colleague also goes along with widely-believed error that `assume_aligned` somehow makes it safe to do incorrectly-aligned accesses, when what it really does is causes the compiler to continue instead of aborting when it sees your code which is obviously wrong.
- uticus 3y ago> I don't know of any portable / standard way to achieve that... Don't you simply make sure all bit fields are adjacent? https://beej.us/guide/bgc/html/split-wide/structs-ii-more-fun-with-structs.html#bit-fields https://beej.us/guide/bgc/html/split-wide/structs-ii-more-fu...