__attribute__的属性aligned,作用是为了设置字节对齐。

aligned是对 变量和结构体进行 字节对齐的属性设置。

通过aligned属性设置(aligned(对齐字节数)),可以显示的设置对齐字节数,如果使用缺省属性(aligned()),编译器会有一个默认的字节对齐数。

aligned特性:aligned属性只能增加对齐字节数,不能减少到比默认对齐字节数还小。

aligned支持的最大对齐字节数由linker决定。

aligned (alignment)

This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

int x __attribute__ ((aligned (16))) = 0;

causes the compiler to allocate the global variable x on a 16-byte boundary.

设置指定大小的对齐格式

You can also specify the alignment of structure fields. For example, to create a double-word aligned int pair, you could write:

struct foo { int x[2] __attribute__ ((aligned (8))); };

This is an alternative to creating a union with a double member that forces the union to be double-word aligned.

As in the preceding examples, you can explicitly specify the alignment (in bytes) that you wish the compiler to use for a given variable or structure field. Alternatively, you can leave out the alignment factor and just ask the compiler to align a variable or field to the maximum useful alignment for the target machine you are compiling for. For example, you could write:

          short array[3] __attribute__ ((aligned));
     

Whenever you leave out the alignment factor in an aligned attribute specification, the compiler automatically sets the alignment for the declared variable or field to the largest alignment which is ever used for any data type on the target machine you are compiling for. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way.

The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well. See below.

Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8 byte alignment, then specifying aligned(16) in an __attribute__ will still only provide you with 8 byte alignment. See your linker documentation for further information.

对变量设置aligned属性:

     int i __attribute__ ((aligned ())) =  ;
printf("address of i:%p\n",&i);
int j __attribute__ ((aligned ())) = ;
printf("address of j:%p\n",&j);
int k __attribute__ ((aligned ())) = ;
printf("address of k:%p\n",&k);
int m __attribute__ ((aligned ())) = ;
printf("address of m:%p\n",&m);

运行结果如下:

address of i:0xbfa43920
address of j:0xbfa43918
address of k:0xbfa43914
address of m:0xbfa43910

对于标准数据类型,它的地址只要是它的长度的整数倍就行了,而非标准数据类型按下面的原则对齐:
  数组 :按照基本数据类型对齐,第一个对齐了后面的自然也就对齐了。 
  联合 :按其包含的长度最大的数据类型对齐。 
  结构体: 结构体中每个数据类型都要对齐。

对于设置结构体成员的属性:

 #include <stdio.h>

 struct p
{
int a;
char b;
char c;
}__attribute__((aligned())) p1; int main(int argc, char** argv)
{
printf("sizeof(int):%d, sizeof(char)=%d\n", sizeof(int), sizeof(char));
printf("sizeof(p1):%d\n", sizeof(p1)); return ;
}

int a占用4字节,char a占用字节,char c占用1字节。那么,

sizeof(int):4, sizeof(char)=1
sizeof(p1):8

 #include <stdio.h>

 struct p
{
int a;
char b;
char c;
}__attribute__((aligned())) p1; struct q
{
int a;
char b;
struct p qn;
char c;
}__attribute__((aligned())) q2; int main(int argc, char** argv)
{
printf("sizeof(int):%d, sizeof(char)=%d\n", sizeof(int), sizeof(char));
printf("sizeof(q2.a)=%d, sizeof(q2.b)=%d,sizeof(q2.qn)=%d, sizeof(c)=%d",sizeof(q2.a), sizeof(q2.b),sizeof(q2.qn), sizeof(q2.c));
printf("sizeof(q2):%d\n", sizeof(q2)); return ;
}

sizeof(int):4, sizeof(char)=1

sizeof(q2.a)=4, sizeof(q2.b)=1,sizeof(q2.qn)=8, sizeof(c)=1sizeof(q2):24

__attribute__系列之aligned的更多相关文章

  1. __attribute__系列之介绍篇

    1.什么是__attribute__? __attribute__机制是GNU C的一大特色,它可以设置函数属性.变量属性和类型属性等.可以通过它们向编译器提供更多数据,帮助编译器执行优化等. 2._ ...

  2. __attribute__系列之cleanup

    cleanup属性:当变量离开它的作用域时,设置的cleanup_function函数将被调用. cleanup (cleanup_function) The cleanup attribute ru ...

  3. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  4. Linux Module

    catalog . 概述 . 使用模块 . 插入和删除模块 . 自动化与热插拔 . 版本控制 1. 概述 模块(module)是一种向Linux内核添加设备驱动程序.文件系统及其他组件的有效方法,而无 ...

  5. Linux内核导出符号宏定义EXPORT_SYMBOL源代码分析

    资源: <include/linux/moudule.h> --. #ifndef MODULE_SYMBOL_PREFIX #define MODULE_SYMBOL_PREFIX &q ...

  6. Linux Module框架【转】

    转自:http://www.cnblogs.com/LittleHann/p/4558719.html catalog 1. 概述 2. 使用模块 3. 插入和删除模块 4. 自动化与热插拔 5. 版 ...

  7. pr_debug、dev_dbg等动态调试一

    内核版本:Linux-3.14 作者:彭东林 邮箱:pengdonglin137@163.com pr_debug: #if defined(CONFIG_DYNAMIC_DEBUG) /* dyna ...

  8. 关于arm 的字节对齐

    一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这 ...

  9. Linux设备驱动程序 之 内核符号表

    insmod使用公共内核符号表来解析模块中未定义的符号.功能内核符号表中包含了所有全局内核项(函数和变量)的地址,这是实现模块化驱动程序所必须的.当模块装载到内核后,它所导出的任何符号都会变成内核符号 ...

随机推荐

  1. [CTSC2017]密钥

    传送门:http://uoj.ac/problem/297 “无论哪场比赛,都要相信题目是水的” 这不仅是HNOI2018D2T3的教训,也是这题的教训,思维定势真的很可怕. 普及组水题,真是愧对CT ...

  2. [BZOJ 1072] 排列perm

    Link: BZOJ 1072 传送门 Solution: 一道直接next_permutation纯暴力就能过的题? 难道2007年时大家都不知道next_permutation这个函数吗 还是用复 ...

  3. AtCoder - 2568 Lotus Leaves

    Problem Statement There is a pond with a rectangular shape. The pond is divided into a grid with H r ...

  4. [Contest20180316]Game

    这题有一个结论:如果他是最强的(⑨),那么线段树最优,如果他是最弱的,那么链状树最优 严格证明可能挺困难,感性理解就是公平赛制让强的人容易赢,极度不公平的赛制能让弱的人有机会反杀 所以我们只改他的能力 ...

  5. 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

    [题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...

  6. Hadoop下大矩阵乘法Version2

    1)使用本方法计算F*B,其中F是1000*1000的矩阵,B是1000*20000的矩阵,使用三个节点的集群,每个节点一个CPU核(集群装在虚拟机里,宿主机只有4个CPU核),每个节点配置一个map ...

  7. jquery给input标签添加data-options属性

    //原生JS实现document.getElementById('startPrice').setAttribute("data-options", "required: ...

  8. Oracle中读取数据一些原理研究

    文章很多摘录了 http://blog.163.com/liaoxiangui@126/blog/static/7956964020131069843572/ 同时基于这篇文章的基础上,补充一些学习要 ...

  9. 【云计算】Docker监控相关资料

    Cloud Insight 是东半球首款次世代系统监控工具:http://www.oneapm.com/ci/docker.html?utm_source=BaiduPaid&utm_medi ...

  10. 搭建svnserve并创建提交钩子

    之前做过很多这个过程了,但每次总有些地方不记得要查资料,现在顺手记录一下,以后好查. 安装svn apt-get install subversion 创建代码仓库 svnadmin create & ...