Explanation: What is thinking about reading in the summer vacation, because there is no computer, it is impossible to verify, so get to the school to verify it. First look at an example, there are three structures defined as follows: struct a {bool b1; bool b2; int I1; int 2; int 3;};
Struct B {Int I1; INT I2; Int I3; BOOL B1; BOOL B2;}; Struct C
{INT II1; BOOL BB1; INT II2; BOOL BB2; INT II3;}; For the two structures defined above, consider the following two problems: 1. What is the theoretical value and the actual value of three structural sizes? ? 2. Are the size of the memory space accounted for the three structures (the actual non-theoretical value)? why? For 1 we can easily result, the theoretical value of the three structural sizes is 14. Because the size of Int in the 32-bit system is 4Byte, BOOL size is 1byte, and the size of the structure is the size of each element, so the theoretical value is 14, as for the actual size, we can also get: #include
#include
INT main () {Int a; Bool B1, B2; Float C; Double D;
COUT << "Variable / T" << "address" << endl; cout << "A / T / T" << & a << endl; cout << "b1 / t / t" << & b1 << ENDL COUT << "B2 / T / T" << & b2 << endl; cout << "c / t / t" << & c << endl; cout << "d / t / t" << & D << ENDL; RETURN 0;} The output is as follows: Variable addressa 0012ff7cb1 0012ff78b20012ff74c 0012ff78b2 0012ff74c 0012ff70d 0012ff68 Next looks at the address assignment of the members of the structure of the above-defined structure: int Main () {a aa; b bb; c cc; cout << " INT Size: / t "<< sizeof (int) <<" Byte "<< Endl; cout <<" BOOL SIZE: / T "<< SizeOf (BOOL) <<" Byte "<< Endl; cout <<" SIZE A: "<< SizeOf (a) << endl; cout <<" address of element in aa: << endl; cout << "b1's address / t / t" << & (aa.b1) << Endl; cout << "B2's address / t / t" << & (aa.b2) << endl; cout << "i1's address / t / t" << & (aa.i1) << Endl; Cout < <"i2's address / t / t" << & (aa.i2) << Endl; cout << "i3's address / t / t" << & (aa.i3) << ENDL;
COUT << "SIZE B:" << sizeof (b) << endl; cout << "address of element in bb:" << endl; cout << "i1's address / t / t" << & (BB. I1) << endl; cout << "i2's address / t / t" << & (bb.i2) << endl; cout << "i3 'address / t / t" << & (bb.i3) << Endl; cout << "b1's address / t / t" << & (bb.b1) << endl; cout << "b2's address / t / t" << & (bb.b2) << Endl; cout < <"SIZE B:" << SIZEOF (C) << Endl; cout << "address of element in cc:" << endl; cout << "i1's address / t / t" << & (cc.i1) << endl; cout << "B1's address / t / t" << & (cc.b1) << Endl; cout << "i2's address / t / t" << & (cc.i2) << ENDL; Cout << "B2's Address / T / T" << & (cc.b2) << Endl; cout << "i3's address / t / t" << & (cc.i3) << end1
return 0;} Run Results: int size: 4bytebool size: 1bytesize a: 16Address of element in aa: b1's address 0012FF70b2's address 0012FF71i1's address 0012FF74i2's address 0012FF78i3's address 0012FF7Csize b: 16Address of element in bb: i1's address 0012FF60i2's address 0012FF64i3's address 0012FF68b1's address 0012FF6Cb2's address 0012FF6Dsize b: 20Address of element in cc: i1's address 0012FF4Cb1's address 0012FF50i2's address 0012FF54b2's address 0012FF58i3's address 0012FF5C results can be explained by the size of the structure due to why the position of the element not the same as the sample sizes. Since the compiler is aligned (aligned) in the storage, the size is different. The address of the two Boolean variables B2 in structure A is not a multiple of 4, which is not contradictory because the structure is regarded as the same type like the internal type, and its internal element is as a whole. Therefore, when declaring a structure, try to arrange each element (from large to small or from small to large), so that it can reduce the space it occupied, but the memory space is no longer considered. The problem can be arranged from the aspects of easy reading. PS. The above is a little idea about the size of the structure, I don't know if it is correct, welcome to correct.