Last active
July 10, 2018 07:15
-
-
Save m-guo/5c1df091798bb2f5508854a3bfd419ea to your computer and use it in GitHub Desktop.
memory alignment
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 <stdio.h> | |
#include <stdlib.h> | |
#include <stddef.h> | |
//memory alignment | |
//alloc a variable at least 4 bytes memory in struct, struct t and struct st have same size, struct ft has 16 bytes | |
struct t { | |
int i; | |
char a; | |
char *b; | |
double d; | |
}; | |
struct st { | |
int i; | |
char a; | |
char a1; | |
char a2; | |
char a3; | |
char *b; | |
double d; | |
}; | |
struct ft { | |
char a; | |
short b; | |
char c; | |
int d; | |
char e[2]; | |
}; | |
int main() | |
{ | |
printf("sizeof(struct t) == sizeof(struct st) == %d, %d\n", sizeof(struct t), sizeof(struct st)); | |
printf("sizeof(struct ft): %d\n", sizeof(struct ft)); | |
printf("i:%d, a:%d, b:%d, c:%d\n", offsetof(struct f, i), offsetof(struct f, a), offsetof(struct f, b), offsetof(struct f, c)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment