The documentation and observed behavior of GCC are consistent with each other.
GCC Docs:
For compatibility with Microsoft Windows compilers, GCC supports a set of #pragma directives that change the maximum alignment of members of structures
[...]
#pragma pack(n) simply sets the new [maximum] alignment
The resulting size of the struct shows that GCC on Linux is aligning the double
on a four-byte (or less) boundary. Four is less than 8.
The documentation and observed behavior of VS are consistent with each other.
VS Docs:
Specifies packing alignment for structure, union, and class members.
[...]
n
[...] The alignment of a member will be on a boundary that is either a multiple of n
or a multiple of the size of the member, whichever is smaller.
The size of a double
is 8, and the specified maximum alignment is 8. The size of the structure shows that VS on Windows aligns the double on an 8-byte boundary, just as it should.
On linux, the alignment seems like 4, not 8. From the doc, it's "For compatibility with Microsoft Windows compilers".
Both compilers accept your pragma. It has no practical effect on either one. This is source compatibility with respect to the pragma.
That the default alignment rules for the two compilers differ could be taken as an incompatibility, but that's not an issue of the pragma. And in this case you can use the pragma to get the same (weaker) alignment of both structures by specifying maximum alignment 4. Or you should be able to get the stronger alignment with GCC on x86 Linux by via the -malign-double
compiler option.
But note well that by looking at VS on Windows and GCC on Linux, you are comparing apples and oranges. There is no binary compatibility consideration across those platforms. Consider trying your experiment with a Windows build of GCC, such as MinGW's. I'm not in a position to test at the moment, but I anticipate that GCC on Windows will conform to Windows alignment conventions by default, and that pragma pack
will affect the layout exactly the same way on the GCC/Windows as it does on VS/Windows.