Created
January 26, 2014 15:24
-
-
Save kkdai/8634346 to your computer and use it in GitHub Desktop.
Talking about alignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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