Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20592
  • 博文数量: 11
  • 博客积分: 560
  • 博客等级: 中士
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-26 11:42
文章分类
文章存档

2011年(1)

2008年(10)

我的朋友
最近访客

分类: C/C++

2008-03-26 17:29:02

对齐可以用
__attribute__ ((aligned (n)))
 
#include

struct A
{
    double l;
    int i;
    int b;
    char c;
} __attribute__ ((aligned(8)));

int main()
{
    struct A m[2];
    printf("%d\n", sizeof(double));
    printf("%d\n", sizeof(struct A));
    printf("%p\n", &m[0]);
    printf("%p\n", &m[1]);
    printf("%d\n", (int)&m[1]-(int)&m[0]);

    printf("%p\n", &(m[0].l));
    printf("%p\n", &(m[1].l));

    return 0;
}
 
另外, 如果不想进行对齐, 可以在编译的时候加上-fpack-struct 这个选项.
或者说声明为
 
struct A
{
    double l;
    int i;
    int b;
    char c;
} __attribute__ ((packed));
阅读(350) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~