3 ms·
You seem to be misunderstanding how padding is applied. As your link says regarding structure alignment: In general, a struct instance will have the alignment
by userbinator 10y ago
You seem to be misunderstanding how padding is applied. As your link says regarding structure alignment:
In general, a struct instance will have the alignment of its widest scalar member.
...which is d if we assume sizeof(double) == 8, so the total size will be rounded up to a multiple of 8. Both your structure examples will be exactly the same size:
0 msg
400 d
408 i
412 [+4 padding]
416 TOTAL
0 d
8 i
12 msg
412 [+4 padding]
416 TOTAL
- shakna 10y ago> On some processors, the addressing of a member is more efficient if its distance from the beginning of the structure is less than 128 bytes. > In the first example, to address the d and i fields using a pointer to the beginning of the structure, an offset of at least 400 bytes is required. > Instead, in the second example, containing the same fields in a different order, the offsets to address d and i are of few bytes, and this allows to use more compact instructions.