Due to alignment. X86 compilers tend to align int types on a 4-byte boundary (for faster memory access), so CHaraICICCC will probably be laid out as follows:
byte 0: \ byte 1: | <--- int i byte 2: | byte 3: / byte 4: <----- char c1 byte 5: \ byte 6: | <--- Padding (wasted bytes) byte 7: / byte 8: \ byte 9: | <--- int j byte 10: | byte 11: / byte 12: <----- char c2 byte 13: <----- char c3 byte 14: <----- char c4
a total of 15 bytes, while CHaraIICCCC will be:
byte 0: \ byte 1: | <--- int i byte 2: | byte 3: / byte 4: \ byte 5: | <--- int j byte 6: | byte 7: / byte 8: <----- char c1 byte 9: <----- char c2 byte 10: <----- char c3 byte 11: <----- char c4
for a total of 12 bytes (without bytes wasted to fill). Of course, it depends on the compiler and depends on your compilation options.
source share