码迷,mamicode.com
首页 > 其他好文 > 详细

关于结构体对齐

时间:2018-01-21 13:39:38      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:快速   硬件   bsp   include   body   数据   c99   har   pos   

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

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

 

关于结构体对齐

标签:快速   硬件   bsp   include   body   数据   c99   har   pos   

原文地址:https://www.cnblogs.com/litifeng/p/8323740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!