结构体成员内存对齐
#include<stdio.h>
struct A
{int A;
};int main()
{struct A a;printf("%d\n",sizeof(a));return 0;
}
運(yùn)行結(jié)果:4
#include<stdio.h> struct A {int a;int b; };int main() {struct A a;printf("%d\n",sizeof(a));return 0; }運(yùn)行結(jié)果:8
#include<stdio.h> struct A {int a;char b; };int main() {struct A a;printf("%d\n",sizeof(a));return 0; }運(yùn)行結(jié)果:8
分析:以結(jié)構(gòu)體中最大的數(shù)據(jù)類型的字節(jié)為標(biāo)準(zhǔn),隨后的基礎(chǔ)數(shù)據(jù)類型都會(huì)跟隨它對(duì)齊
#include<stdio.h> struct A {char a; short b; int c;short d;char e; };int main() {struct A a;printf("%d\n",sizeof(a));return 0; }運(yùn)行結(jié)果:12
| a | ? | b | |
| c | |||
| d | e | ? | |
| 13 | 14 | 15 | 16 |
32位計(jì)算機(jī)(主流):
long?????? 8
int????????? 4
short????? 2
char?????? 1
#include<stdio.h> struct A {char a; short b; char c;int d;long long e; };int main() {struct A a;printf("%d\n",sizeof(a));return 0; }運(yùn)行結(jié)果:24
| a | ? | b |
| c | ? | |
| d | ||
| e | ||
轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11172019.html
總結(jié)
- 上一篇: 第一个试管婴儿多大了
- 下一篇: C库函数-fgets()