Trecord2 =RecordA: integer; B: integer; C: integer;End; Trecord1=RecordA: byte;CaseIntegerOf0: (A1: trecord2 );1: (A2: int64 );End;
The first step is to know the calculation method of the struct size:
The struct size is equal to the offset of the last member plus the size of the last member,
Offset refers to the difference between the address of the member in the struct variable and the address of the struct variable. The offset of the first Member is 0.
In fact, due to address alignment requirements when storing variables, the compiler will follow two principles during program Compilation:
1. The member offset in the struct variable must be an integer multiple of the member size (0 is considered an integer multiple of any number)
2. the struct size must be an integer multiple of the size of all members.
Sizeof (trecord2) = 12
A: The offset is 0,
B: The offset is 4.
C: The offset is 8.
The size is the offset of the last variable + the size of the last Member 8 + 4 = 12
Sizeof (trecord1) = 24;
Struct contains struct. Expand this struct.
Trecord1 =RecordA: byte; A2: int64;End; Trecord1=RecordA: byte; a2.trecord2. A: integer; a2.trecord2. B: integer; a2.trecord2. C: integer;End;
To determine the offset of A2, the offset of A2 should be
1. The member offset in the struct variable must be an integer multiple of the member size.
The offset of A2 should be 8.
Then determine the A2 size. In the first case, the A2 size is 8, and in the second case is 12.
The result is 8 + 12.
2. the struct size must be an integer multiple of the size of all members.
20 is not an integer multiple of 8, so it should be 24
Size of struct