关于结构体对齐

为了硬件能够快速访问,数据在内存中要对齐(参考集装箱)。这里主要说一下结构体的对齐。

1、结构体内元素的大小,又下一个字节决定。

2、系统默认4个字节对齐方式。

#include <stdio.h>
struct s{
    char b;
    int c;
};
int main(){
    struct s s1;
    s1.b='t';
    s1.c=12;
    printf("s1 size is %zu\n",sizeof(s1));
    char *p1=(void *)&s1;
    printf("s1.b=%c\n",*p1);
    int *p2=(void *)&s1+1;
    printf("s1.c=%d\n",*p2);
    int *p3=(void *)&s1+4;
    printf("s1.c=%d\n",*p3);
   
    return 0;
}

输出结果:

fly@noi:~$ make t4
cc -g -std=c99    t4.c   -o t4
fly@noi:~$ ./t4
s1 size is 8
s1.b=t
s1.c=201326592
s1.c=12

 

转载于:https://www.cnblogs.com/litifeng/p/8323740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值