__attribute__

__attribute__ 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )

aligned:

 

struct S
{
short b[3];
}__attribute__ ((aligned (2)));

 

int main()
{
  struct S s;
  printf("size = %lu\n",sizeof(s));
  return 0;
}

aligned (n) :n必须的2的幂

结果: aligned (2)  sizeof(s)= 6

aligned (4)  sizeof(s)= 8

aligned (16)  sizeof(s)= 16

如果aligned 后面不紧跟一个指定的数字值,那么编译器将依据你的目标机器情况使用最大最有益的对齐方式

struct S 
{
  char a;
}__attribute__ ((aligned ));

struct S s;sizeof(s) = 16;//一般情况下都等于16(64bit机器上)

----------------------------------------------------------------------------------------------------------

packed:

 使用packed 可以减小对象占用的空间。

struct S
{
char a;
int c;
short b;
}__attribute__((packed));
int main()
{
struct S s;
printf("size = %lu\n",sizeof(s));
return 0;
}

sizeof(s) = 7;//packed是连续存储的中间没有间隔

--------------------------------------------------------------------------

函数属性(Function Attribute)

函数属性可以帮助开发者把一些特性添加到函数声明中,从而可以使编译器在错误检查方面的功能更强大

format的语法格式为:

format (archetype, string-index, first-to-check)
其中,“archetype”指定是哪种风格;“string-index”指定传入函数的第几个参数是格式化字符串;

“first-to-check”指定从函数的第几个参数开始按上述规则进行检查。
具体的使用如下所示:
__attribute__((format(printf, a, b)))
__attribute__((format(scanf, a, b)))
注:以下是从0开始数

a:第几个参数为格式化字符串(format string);
b:参数集合中的第一个,即参数“…”里的第一个参数在函数参数总数排在第几。

 

#include <stdio.h>
#include <stdarg.h>

#if 1
#define CHECK_FMT(a, b) __attribute__((format(printf, a, b)))
#else
#define CHECK_FMT(a, b)
#endif

void TRACE(const char *fmt, ...) CHECK_FMT(1, 2);

void TRACE(const char *fmt, ...)
{
va_list ap;

va_start(ap, fmt);

(void)printf(fmt, ap);

va_end(ap);
}

int main(void)
{
TRACE("iValue = %d\n", 6);
TRACE("iValue = %d\n", "test");

return 0;
}

加了__attribute__ format之后会有警告,不加没有警告

总结:__attribute__ format的用法是为了能更容易的检查出不定数量参数的函数错误

---------------------------------------------------------------------------------------------------------------

__attribute__ noreturn
该属性通知编译器函数从不返回值,当遇到类似函数需要返回值而却不可能运行到返回值处就已经退出来的情况,该属性可以避免出现错误信息。C库函数中的abort()和exit()的声明格式就采用了这种格式,如下所示:

extern void exit(int)      __attribute__((noreturn));extern void abort(void) __attribute__((noreturn)); 

 

#include <stdio.h>
#include <stdlib.h>

 

void myexit() /*__attribute__((noreturn))*/;
//void myexit() __attribute__((noreturn));

 

void myexit()
{
printf("myexit\n");
exit(0);
}
int foo(int a)
{
if(a == 1)
myexit();
else
return 0;
}
int main()
{
int a = 1;
foo(a);
return 0;
}

此函数有警告,但是能正确执行:

noreturn.c: In function ‘foo’:
noreturn.c:18:1: warning: control reaches end of non-void function [-Wreturn-type]

为了消除这种警告,要是有__attribute__((noreturn))

 

 

--------------------------------------------------------------------------------------------------------------

 

posted @   MoonXu  阅读(714)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示