Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created January 26, 2014 15:24
Show Gist options
  • Save kkdai/8634346 to your computer and use it in GitHub Desktop.
Save kkdai/8634346 to your computer and use it in GitHub Desktop.
Talking about alignment
int main(int argc, const char * argv[])
{
//#pragma pack(push)
#pragma pack(4)
struct align_test
{
char x; //1 byte
int y; //4 bytes
short int z; //2bytes
};
#pragma pack()
struct align_test test;
printf("test size is %d\n", sizeof(test)); // Size should be 1+4+2 but it will print 4+4+4=12
printf("x size is %d\n", sizeof(test.x));
printf("y size is %d\n", sizeof(test.y));
printf("z size is %d\n", sizeof(test.z));
printf("address of test is %p\n", &test);
printf("address of x is %p\n", &test.x);
printf("address of y is %p\n", &test.y);
printf("address of z is %p\n", &test.z);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment