__attribute__((constructor))和 __attribute__((destructor))
__attribute__((constructor)):放在main函数之前执行的函数的前面。
__attribute__((destructor)):放在main函数之后执行的函数的前面。
测试代码
#include <stdio.h>
#include <stdlib.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
__attribute__((constructor))
void before_main(void){
DEBUG_INFO("before_main");
}
__attribute__((destructor))
void after_main(void){
DEBUG_INFO("after_main");
}
int main(int argc, char **argv){
DEBUG_INFO("main");
return 0;
}
执行结果:
__attribute__((noreturn))
不返回,不是返回void,也就是说被它修饰的函数,最后会exit(0)程序。
#include <stdio.h>
#include <stdlib.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
__attribute__((noreturn))
void test()
{
exit(0);
}
int main(int argc, char **argv){
DEBUG_INFO("main");
test();
return 0;
}
执行结果:
__attribute__((mode(HI)))和__attribute__((mode(QI)))
#include <stdio.h>
#include <stdlib.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
typedef unsigned int myint_hi __attribute__((mode(HI)));
typedef unsigned int myint_qi __attribute__((mode(QI)));
int main(int argc, char **argv){
DEBUG_INFO("mode(HI):sizeof(myint_hi) = %d\n",sizeof(myint_hi));
DEBUG_INFO("mode(QI):sizeof(myint_qi) = %d\n",sizeof(myint_qi));
return 0;
}
执行结果:
__attribute__((packed))
#include <stdio.h>
#include <stdlib.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
struct private_data_1{int a;char b;float c} __attribute__((packed));
struct private_data_2{int a;char b;float c};
int main(int argc, char **argv){
DEBUG_INFO("sizeof(struct private_data_1) = %d\n",sizeof(struct private_data_1));
DEBUG_INFO("sizeof(struct private_data_2) = %d\n",sizeof(struct private_data_2));
return 0;
}
执行结果:
实验解析:
两个相同的结构体,正常情况下会4字节内存对齐,大小12个字节
__attribute__((aligned(1)))
#include <stdio.h>
#include <stdlib.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ " format "\n" \
,__func__,__LINE__ \
, ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
struct private_data_1{int a;long long d;char b;float c;} __attribute__((packed));
struct private_data_2{int a;long long d;char b;float c;};
struct private_data_3{int a;long long d;char b;float c;} __attribute__((aligned(1)));
struct private_data_4{int a;long long d;char b;float c;} __attribute__((aligned(2)));
struct private_data_5{int a;long long d;char b;float c;} __attribute__((aligned(4)));
struct private_data_6{int a;long long d;char b;float c;} __attribute__((aligned(8)));
int main(int argc, char **argv){
DEBUG_INFO("sizeof(struct private_data_1) = %ld\n",sizeof(struct private_data_1));
DEBUG_INFO("sizeof(struct private_data_2) = %ld\n",sizeof(struct private_data_2));
DEBUG_INFO("sizeof(struct private_data_3) = %ld\n",sizeof(struct private_data_3));
DEBUG_INFO("sizeof(struct private_data_4) = %ld\n",sizeof(struct private_data_4));
DEBUG_INFO("sizeof(struct private_data_5) = %ld\n",sizeof(struct private_data_5));
DEBUG_INFO("sizeof(struct private_data_6) = %ld\n",sizeof(struct private_data_6));
return 0;
}
执行结果:
晕,该不会写错了吧。
消除未使用的变量的警告
int main(){
int i = 0;
return 0;
}
这段代码会报警
warning: unused variable ‘i’ [-Wunused-variable]
int i = 0;
消除报警的写法
int i __attribute__((unused)) = 0;
或者
__attribute__((unused))
int i = 0;