【C/C++】__attribute__ 用法

写代码遇到了__attribute__简单记录一下用法。

__attribute__ 是一个用于在声明时指定一些特性的编译器指令,它可以让我们进行更多的错误检查和高级优化工作。官方文档:https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

In GNU C and C++, you can use function attributes to specify certain function properties that may help the compiler optimize calls or check code more carefully for correctness. For example, you can use attributes to specify that a function never returns (noreturn), returns a value depending only on the values of its arguments (const), or has printf-style arguments (format).

暂时只记录用到的:
1.
__attribute__((aligned (n))),让所作用的结构成员对齐在n字节自然边界上。如果结构中有成员的长度大于n,则按照最大成员的长度来对齐。
2.
__attribute__ ((packed))让指定的结构结构体按照一字节对齐,即取消对齐。

typedef struct {
......
}__attribute__ ((packed)) Header;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值