gcc __attribute__ ((packed)) || __attribute__ ((aligned(4))) 2
来源:互联网 发布:微机室网络卡 编辑:程序博客网 时间:2024/05/17 04:33
此属性可以强制修改gcc的对齐方式
一般软件的2进制协议中,会定义一系列的规范,32位机一般会定义4字节对齐的协议,这样对于32位机来说速度是最快的。
最近发现gcc一个问题,在一个结构体包含unsignd long long (64位)类型时会导致结构体8字节对齐,且__attribute__ ((aligned(4)))
竟然无效
本例子用的x86 编译器版本
#gcc --versiongcc (GCC) 4.1.3 20080704 (prerelease) (Debian 4.1.2-25)Copyright (C) 2006 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# arm-linux-gcc --versionarm-linux-gcc ((gcc4.4-290+uclibc_0.9.32.1+eabi+linuxpthread)) 4.4.1Copyright (C) 2009 Free Software Foundation, Inc.This is free software; see the source for copying conditions. There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
直接上例子程序
#include <stdio.h>typedef unsigned long long ullong;typedef struct { char a; ullong b; int c;/*} __attribute__ ((packed)) test ;*//*} __attribute__ ((aligned (4))) test ;*/} test;int main ( int argc, char *argv[] ){ printf("file:%s, line:%d, sizeof(test):%d\n", __FILE__, __LINE__, sizeof(test)); return 0;}x86 32运行结果
file:test.c, line:15, sizeof(test):16arm contex A9 运行结果
file:test.c, line:15, sizeof(test):24
结果说明在arm编译器成了8字节对齐 x86编译器还是4字节对齐
为了让该结构体4字节对齐,我们采用
__attribute__ ((aligned (4)))
强制对齐
输出结果还是
file:test.c, line:15, sizeof(test):24无奈使用单字节对齐
__attribute__ ((aligned (4))) test输出结果为
file:test.c, line:15, sizeof(test):13
这样虽然解决了我们4字节对齐的问题,但是这样 我们得严格注意结构体对齐的问题。
- gcc __attribute__ ((packed)) || __attribute__ ((aligned(4))) 2
- __attribute__ ((aligned (n), packed))
- __attribute__ ((aligned (n), packed))
- __attribute__ ((aligned (n), packed))
- __attribute__ ((aligned (n), packed))
- gcc 中的"__attribute__ ((packed))"
- GCC __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__((packed))
- __attribute__((packed))
- __attribute__((packed))
- __attribute__ ((packed))
- __attribute__ ((packed))
- __attribute__((packed))
- __attribute__((packed))
- vc下和gcc的结构体紧凑对齐格式存储【#pragma pack(push,1)、__attribute__ ((packed, aligned(1)))】
- 求最小公倍数和最大公约数的总结
- 收到短信了,我该做点什么?
- 《Tomcat权威指南》读书笔记
- ListView 动态删除添加Item方法
- Web服务的创建,发布以及客户端的调用
- gcc __attribute__ ((packed)) || __attribute__ ((aligned(4))) 2
- 2012.6.26
- ubuntu乱码汇总
- linux ,Android基础知识总结
- 基于VM的MerOBS搭建
- oracle学习资料pdf
- Fedora 15/16 安装后需要做的28件事
- JavaScript中的面向对象
- 我的博客组件被删,博文被多次删除,啥意思