I've been asked in a recent interview about C++ struct fields alignment and theoretized that C and C++ follows the same strategy in struct packing.
Hovewer, it was the wrong assumption. The interviewer said that in general C and C++ are packing structs in different ways and we should never expect the opposite. IMHO it's strange statement. There is no pack "C"
qualifier for structs in C++ for using in bilingual C/C++ header files.
So in practice it could mean that you can't create a struct in C++ and pass it to a C library because in general its fields will be aligned in a different way and have different offsets. But, in fact, most programmers seriously rely on this interoperability up to the point when they convert a pointer to a C POD struct to a reference to C++ wrapper around this struct with some helper methods. Can you please clarify this matter?
extern "C" { ...
), that should be identical. I would not bet on the reverse, however. – Progenitorpack
edstruct
s which read anint
byte-wise and assembles them in registers (and vice-versa for writes). Quite the same as manual (de)serialisation. And thepack
directive is not standard anyway and not supported by all compilers. – Progenitor1 octet
? IIRC, there was only one CPU Family which had a bit-alignment, the TMS34010/20. So, of course the CPU mandates aligmnent, thus padding. Anyway, the current ABIs for x86 also mandate a specific alignment for performance reasons. Admitted, this is not mandated, but _recommended. (although without it you will loose performance). – Progenitor