On Wednesday, 11 June 2014 at 07:22:51 UTC, deadalnix wrote:
On Wednesday, 11 June 2014 at 06:30:26 UTC, Walter Bright wrote:
On 6/10/2014 11:11 PM, deadalnix wrote:
On Wednesday, 11 June 2014 at 04:11:53 UTC, Walter Bright
wrote:
Hmm, this could have serious problems with the following:
S1 s1;
S2 s2;
s2.c = 3;
memcpy(&s2.s1, &s1, sizeof(S1)); // Oops! stomped on s2.c
Yes, that is why they do it only in specific condition (ie
when the thing use
C++ specific feature and not C one).
I don't understand - didn't you say this was expressible as C
structs? Aren't those supposed to be compatible with C?
struct S1 {
int a;
private:
char b;
};
struct S2 : S1 {
char c;
};
S2 is 8 bytes. If you remove private in S1, then S2 becomes 12
bytes. S1 is not a "C" struct anymore and do not need to follow
the standard layout.
This is pretty strange behavior.
At least on Windows I can not confirm this.
Visual Studio 2013, Intel Compiler and Clang for windows have the
same consistent behavior here.
private do NOT affect struct size.
But there is a parameter in Visual Studio that will affect it
called "Struct Member Alignment"
http://msdn.microsoft.com/en-us/library/xh3e3fd0.aspx
1 Byte (/Zp1) sizeof(S1)=5, sizeof(S2)=6
2 Byte (/Zp2) sizeof(S1)=6, sizeof(S2)=8
4 Byte (/Zp4) sizeof(S1)=8, sizeof(S2)=12
8 Byte (/Zp8) sizeof(S1)=8, sizeof(S2)=12 this is the default.
Of course the same can be done with #pragma pack(?) .
There is also __declspec(align(?)) and in C++11 alignas(?) but
its behavior is different from #pragma pack(?) .