linux i386系统中struct 的数据对齐(From: todaygood.cublog.cn)

Linux i386 Struct对齐
本文探讨了Linux i386系统中struct类型的内存对齐方式,并通过实例展示了gcc编译器如何进行对齐处理。通过对不同数据类型结构体的分析,明确了该平台的最大对齐单位为4字节。
linux i386系统中struct 的数据对齐
在linux 系统中的应用程序,struct 并非是采用自然对齐,而是gcc强制对齐

何为自然对齐:在数据项大小的整数倍(例如:8字节数据项存入8的整数倍的地址)的地址处存储
                           数据项。

#include <stdio.h>
#include <sys/utsname.h>
#include <linux/types.h>

/*
 * Define several data structures, all of them start with a lone char
 * in order to present an unaligned offset for the next field
 */
struct c   {char c;  char      t;} c;
struct s   {char c;  short     t;} s;
struct i   {char c;  int       t;} i;
struct l   {char c;  long      t;} l;
struct ll  {char c;  long long t;} ll;
struct p   {char c;  void *    t;} p;
struct u1b {char c;  __u8      t;} u1b;
struct u2b {char c;  __u16     t;} u2b;
struct u4b {char c;  __u32     t;} u4b;
struct u8b {char c;  __u64     t;} u8b;

int main(int argc, char **argv)
{
    struct utsname name;

    uname(&name); /* never fails :) */
    printf("arch  Align:  char  short  int  long   ptr long-long "
       " u8 u16 u32 u64/n");
    printf(       "%-12s  %3i   %3i   %3i   %3i   %3i   %3i      "
       "%3i %3i %3i %3i/n",
       name.machine,
       /* note that gcc can subtract void * values, but it's not ansi */
       (int)((void *)(&c.t)   - (void *)&c),
       (int)((void *)(&s.t)   - (void *)&s),
       (int)((void *)(&i.t)   - (void *)&i),
       (int)((void *)(&l.t)   - (void *)&l),
       (int)((void *)(&p.t)   - (void *)&p),
       (int)((void *)(&ll.t)  - (void *)&ll),
       (int)((void *)(&u1b.t) - (void *)&u1b),
       (int)((void *)(&u2b.t) - (void *)&u2b),
       (int)((void *)(&u4b.t) - (void *)&u4b),
       (int)((void *)(&u8b.t) - (void *)&u8b));
    return 0;
}

[root@hujunlinux driver_pratice]# ./dataalign
arch  Align:  char  short  int  long   ptr long-long  u8 u16 u32 u64
i686                  1     2          4     4     4     4               1   2     4      4

可见最大的对齐单位是4




int main()
{      
        struct A{  char c;
                   long long  b;
                }one;
               
        printf("sizeof(one)=%d,sizeof(c)=%d/n",sizeof(one),
        (int)((void*)(&(one.b))-(void*)(&one))
        );
       
       
        return 0;

}
 ./charSize
sizeof(one)=12,sizeof(c)=4


int main()
{      
        struct A{  char c;
                 short  b;
                }one;
               
        printf("sizeof(one)=%d,sizeof(c)=%d/n",sizeof(one),
        (int)((void*)(&(one.b))-(void*)(&one))
        );
       
       
        return 0;

}

./charSize
sizeof(one)=4,sizeof(c)=2


int main()
{      
        struct A{  char c;
                   int  b;
                }one;
               
        printf("sizeof(one)=%d,sizeof(c)=%d/n",sizeof(one),
        (int)((void*)(&(one.b))-(void*)(&one))
        );
       
       
        return 0;

}
./charSize
sizeof(one)=8,sizeof(c)=4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值