__attribute__系列之介绍篇
1.什么是__attribute__?
__attribute__机制是GNU C的一大特色,它可以设置函数属性、变量属性和类型属性等。可以通过它们向编译器提供更多数据,帮助编译器执行优化等。
2.__attribute__语法格式?
https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Attribute-Syntax.html#Attribute-Syntax
__attribute__ 书写特征是:__attribute__ 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__ 参数。
__attribute__ 语法格式为:__attribute__ ((attribute-list))
其位置约束为:放于声明的尾部“ ;” 之前。
An attribute specifier is of the form __attribute__ ((
attribute-list))
. An attribute list is a possibly empty comma-separated sequence of attributes, where each attribute is one of the following:
- Empty. Empty attributes are ignored.
- A word (which may be an identifier such as
unused
, or a reserved word such asconst
). - A word, followed by, in parentheses, parameters for the attribute. These parameters take one of the following forms:
- An identifier. For example,
mode
attributes use this form. - An identifier followed by a comma and a non-empty comma-separated list of expressions. For example,
format
attributes use this form. - A possibly empty comma-separated list of expressions. For example,
format_arg
attributes use this form with the list being a single integer constant expression, andalias
attributes use this form with the list being a single string constant.
- An identifier. For example,
3.函数属性:
https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html#Function-Attributes
__attribute__ format
__attribute__ noreturn
__attribute__ const
4.变量属性:
https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html#Variable-Attributes
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.
cleanup (
cleanup_function)
The cleanup
attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.
If -fexceptions is enabled, then cleanup_function will be run during the stack unwinding that happens during the processing of the exception. Note that the cleanup
attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.
packed
5.类型属性:
https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Type-Attributes.html#Type-Attributes
__attribute__系列之介绍篇的更多相关文章
- k8s入门系列之介绍篇
•Kubernetes介绍1.背景介绍 云计算飞速发展 - IaaS - PaaS - SaaS Docker技术突飞猛进 - 一次构建,到处运行 - 容器的快速轻量 - 完整的生态环境2.什么是ku ...
- 《java.util.concurrent 包源码阅读》09 线程池系列之介绍篇
concurrent包中Executor接口的主要类的关系图如下: Executor接口非常单一,就是执行一个Runnable的命令. public interface Executor { void ...
- 一、Kubernetes系列之介绍篇
•Kubernetes介绍 1.背景介绍 云计算飞速发展 - IaaS - PaaS - SaaS Docker技术突飞猛进 - 一次构建,到处运行 - 容器的快速轻量 - 完整的生态环境 2.什 ...
- k8s 入门系列之介绍篇
•Kubernetes介绍1.背景介绍 云计算飞速发展 - IaaS - PaaS - SaaS Docker技术突飞猛进 - 一次构建,到处运行 - 容器的快速轻量 - 完整的生态环境2.什么是ku ...
- 使用ADMT和PES实现window AD账户跨域迁移-介绍篇
使用 ADMT 和 pwdmig 实现 window AD 账户跨域迁移系列: 介绍篇 ADMT 安装 PES 的安装 ADMT:迁移组 ADMT:迁移用户 ADMT:计算机迁移 ADMT:报告生成 ...
- Kubernetes系列之Helm介绍篇
本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 介绍 Helm 是 Deis 开发的一个用于 Kubernetes 应用的包管理工具,主要用来 ...
- SpringBoot 系列教程之编程式事务使用姿势介绍篇
SpringBoot 系列教程之编程式事务使用姿势介绍篇 前面介绍的几篇事务的博文,主要是利用@Transactional注解的声明式使用姿势,其好处在于使用简单,侵入性低,可辨识性高(一看就知道使用 ...
- spring cloud系列教程第一篇-介绍
spring cloud系列教程第一篇-介绍 前言: 现在Java招聘中最常见的是会微服务开发,微服务已经在国内火了几年了,而且也成了趋势了.那么,微服务只是指spring boot吗?当然不是了,微 ...
- 【Windows编程】系列第五篇:GDI图形绘制
上两篇我们学习了文本字符输出以及Unicode编写程序,知道如何用常见Win32输出文本字符串,这一篇我们来学习Windows编程中另一个非常重要的部分GDI图形绘图.Windows的GDI函数包含数 ...
随机推荐
- [BZOJ1833][ZJOI2010]Count数字计数(DP)
数位DP学傻了,怎么写最后都写不下去了. 这题严格上来说应该不属于数位DP?只是普通DP加上一些统计上的判断吧. 首先复杂度只与数的位数$\omega$有关,所以怎么挥霍都不会超. f[i][j][k ...
- bzoj 2660: [Beijing wc2012]最多的方案
Time Limit: 5 Sec Memory Limit: 128 MB Submit: 617 Solved: 361[Submit][Status][ ...
- [SRM570]TheTiles
题意:给一个$n\times m$的网格,对这个网格黑白染色,左上角为黑色.现在要用一些大小为$3$的L型图形覆盖这个网格,要求不能重复覆盖同一个格子,不能覆盖到障碍,L型可以进行旋转,但转角处格子必 ...
- [ZOJ3522]Hide and seek
题意:给一棵带边权的树,多次询问$(x,y,l)$表示如果加一条连接$x$和$y$的长为$l$的边,所有点到$x$和到$y$的最短路减少了多少 先把题目中的图放上来(雾 考虑用lct维护,先把路径提出 ...
- 【DFS】URAL - 2104 - Game with a Strip
大概就是dfs?当前区间(l,r)的答案可以由(l,m)和(m+1,r)区间推出,如果某个区间已经完全被某种颜色覆盖,那么就返回该颜色.否则按照递归层数判定,奇数层Alice占优势,只需左右区间中一者 ...
- FCL研究-LINQ-System.Linq Enumerable
.net 里面集合操作极为方便,尤其是实现了IEnumerable接口的集合,一直在使用,系统的研究一下集合的操作也是极好的. 类型 操作符名称 投影操作符 Select,SelectMany 限制操 ...
- POP3、IMAP、SMTP邮件协议的理解
一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 CSDN博客链接: http://blog.csdn.net/my_confesser 正文 今天入职配置OutLook的时候,看到公司的 ...
- MythXinWCF通用宿主绿色版发布(一键启动,方便快捷)
这是我开发的一款免费的,绿色的WCF宿主工具,一键启动,方便快捷,省去了安装和配置IIS的麻烦,也不需要配置文件. 该工具运行环境为.Net Framework 4.5.1(这个是必装的,win10自 ...
- Javascript高级程序设计-问答模式
1.谈谈javascript数组排序方法sort()的使用,重点介绍sort()参数的使用及其内部机制 sort的实现的功能类似JAVA的比较器,数据排序从多维数组的第一维开始排序可以自己定义排序方法 ...
- C#的Xamarin开发小米盒子应用并以WCF实现微信通知
对于熟悉C#语言的开发人员而言,用Xamarin开发Android应用也是一个不错的选择.小米盒子是Android系统.当然也就能够使用Xamarin来开发.首选来看效果图. watermark/2/ ...