3 ms·
In my first job, there was an interesting bug introduced by an un-terminated pragma-pack in a header file. Because not every structure was packed, depending on
by saalweachter 7y ago
In my first job, there was an interesting bug introduced by an un-terminated pragma-pack in a header file.
Because not every structure was packed, depending on your include order, some structures would be packed differently in different compilation units.
Except, for the only structures this happened for, the packed packing was coincidentally the same as the default packing ... when the program was compiled 32-bits. Attempting to switch from a 32-bit executable to a 64-bit executable resulted in mysterious segfaults as different compilation units disagreed on where different fields were.
Fun times!
- gambiting 7y ago>> some structures would be packed differently in different compilation units. <shivers violently>
- karlding 7y agoThis is why I always use static_assert [0] to assert on the struct size in projects using C11+, or use a macro to create my own (using the negative array size trick) to cause a compile error if I can't use C11 as a sanity check for these cases. This saves a lot of potential headaches. [0] https://en.cppreference.com/w/c/language/_Static_assert https://en.cppreference.com/w/c/language/_Static_assert