#include <stdio.h>
struct X
{
short s;
int i;
char c;
};
struct Y
{
int i;
char c;
short s;
};
struct Z
{
int i;
short s;
char c;
};
int main() {
printf("%lu %lu %lu
", sizeof(struct X), sizeof(struct Y), sizeof(struct Z));
return 0;
}
The above 回返
12 8 8
demonstrating that a structure s alignment depends on the order its members are declared. Are there any compiler settings to optimally reorder these declarations (for minimum struct size)? What are some general rules of practice that ensure optimal alignment as projects scale?