NetApp Interview Question
Software Engineer / Developersbyte padding will be done by compiler to reduce number of machine cycles to read. if the integers are stored in the address which is divisible by its size (say 4) then only cpu can read whole 4 bytes in a single cycle other wise cpu machine cycles will be more heance byte padding will be done.
Say we don't have pad, then say c start at address 0, a will start at address 1 ,d will start at address 5, so when we want to read a, we need read an integer from address 0 first for the _first_ 3 bytes of a, then we need read from address 4 to read the last byte;
Datatype misallignment error might happen due to this.
Suppose that alignment is 4 bytes(word size).
- Maxim Stepanenko March 09, 2011struct A{
char c; => 1 byte + 3 bytes alignment
int a; => 4 bytes
char d; => 1 byte + 3 bytes alignment
};
"A" total: 12 bytes
struct B{
int a; => 4 bytes
char c; => 1 byte
char d; => 1 byte + 2 bytes alignment
};
"B" total: 8 bytes