Pergunta de entrevista da empresa Apple

Showed a struct definition in C and asked its size?

Respostas da entrevista

Sigiloso

29 de out. de 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

Sigiloso

10 de nov. de 2009

Consider padding

1

Sigiloso

1 de ago. de 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).