Apple Interview Question

Showed a struct definition in C and asked its size?

Interview Answers

Anonymous

Oct 29, 2010

In a question like this, its important to point out that the size if somewhat compiler/ platform ABI dependent. For example, some platforms require a 64-bit int to be aligned to 64-bits, some do not. Some platforms also have 32-bit longs, while some have 64-bit longs. Once the size of types are agreed, remember the compiler will insert padding to make sure types are aligned, and the sizeof() the structure is always rounded up to the largest member size. For example, a struct containing a 64-bit long will always be a multiple of 8 bytes in size.

4

Anonymous

Nov 10, 2009

Consider padding

1

Anonymous

Aug 1, 2011

The size of struct is all memebers size, not the largest member size. for example struct s { int n; float f; double d; } then sizeof (s) is sizeof (int + float+double).