This question of the textbook online a lot, I will not say more here to give a: Click to open the link
This question is the most likely to cause a dispute, why? With the platform or the compiler has a relationship, first look at the following code:
#include <iostream>using namespace std;union u1{ char a[9]; int b;}; Union u2{ U1 A; Double b;}; Union u3{ Char a[10]; Double b;}; struct s1{ char a[13]; Double C;}; struct s2{ S1 A; U1 b;}; struct s3{ char b; int C; Char d[13];}; int main () { cout << "U1:" << sizeof (U1) << Endl; cout << "U2:" << sizeof (U2) << Endl; cout << "U3:" << sizeof (U3) << Endl; cout << "S1:" << sizeof (S1) << Endl; cout << "S2:" << sizeof (S2) << Endl; cout << "S3:" << sizeof (S3) << Endl; return 0;}
I tested it in two ways, with the following results:
(1) The results of the g++ compilation under Ubuntu system are as follows:
(2) Under Windows System, the results of CMake compilation are as follows:
Specifically not very clear, may be Ubuntu did some optimization, welcome to discuss
Cross-platform discussion of union and struct type sizes