Created
November 6, 2016 00:55
-
-
Save osa1/76fb07be41c2a59db8627806ca58b564 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdint.h> | |
#include <stdio.h> | |
// Most strictly aligned component has alignment 1, so all fields should be | |
// alignment by 1 | |
struct S1 | |
{ | |
uint8_t f1; | |
uint8_t f2; | |
}; | |
// Most strictly aligned component has alignment 4, so there should be 3-byte | |
// space between f1 and f2. | |
struct S2 | |
{ | |
uint8_t f1; | |
uint32_t f2; | |
}; | |
// Same as S2, but we tell gcc to not align fields. | |
struct __attribute__((__packed__)) S3 | |
{ | |
uint8_t f1; | |
uint32_t f2; | |
}; | |
int main() | |
{ | |
printf("sizeof(struct S1) = %d\n", sizeof(struct S1)); | |
printf("sizeof(struct S2) = %d\n", sizeof(struct S2)); | |
printf("sizeof(struct S3) = %d\n", sizeof(struct S3)); | |
return 0; | |
} |
This file contains hidden or 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
> ~ ./a.out | |
sizeof(struct S1) = 2 | |
sizeof(struct S2) = 8 | |
sizeof(struct S3) = 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment