C基础及不常见的用法

1.结构体中最后一个数组长度为零

参考1
struct tag1
{
int a;
int b;
}__attribute ((packed));

struct tag2
{
int a;
int b;
char *c;
}__attribute ((packed));

struct tag3
{
int a;
int b;
char c[0];
}__attribute ((packed));

struct tag4
{
int a;
int b;
char c[1];
}__attribute ((packed));

注意:__attribute ((packed)) 是为了告诉编译器强制不进行4字节对齐
大小:
sizeof tag1 = 8
sizeof tag2 = 12
sizeof tag3 = 8
sizeof tag4 = 9

tag3中char c[0]中的c并不是指针,是一个偏移量,这个偏移量指向的是a、b后面紧接着的空间,所以它其实并不占用任何空间。
好处:
使用char c[0]扩充空间比较方便,释放起来也比较方便
如:
struct pppoe_tag {
__u16 tag_type;
__u16 tag_len;
char *tag_data;
} __attribute ((packed));

struct pppoe_tag *sample_tag;
__u16 sample_tag_len = 10;
方法一:
sample_tag = (struct pppoe_tag *)malloc(sizeof(struct pppoe_tag));
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data = malloc(sizeof(char)*sample_tag_len);
sample_tag->tag_data[0]=…
释放时:
free(sample_tag->tag_data);
free(sample_tag);

方法二:
sample_tag = (struct pppoe_tag *)malloc(sizeof(struct pppoe_tag)+sizeof(char)*sample_tag_len);
sample_tag->tag_len = sample_tag_len;
sample_tag->tag_data = ((char *)sample_tag)+sizeof(struct pppoe_tag);
sample_tag->tag_data[0]=…
释放时:
free(sample_tag);
所以无论使用那种方法,都没有char tag_data[0]这样的定义来得方便。

2.likely 、unlikely、__builtin_expect

参考1

(1)作用:编译器优化,提高程序运行效率
这个指令是gcc引入的,作用是允许程序员将最有可能执行的分支告诉编译器。这个指令的写法为:__builtin_expect(EXP, N)。
意思是:EXP==N的概率很大。

(2)常见方式
一般的使用方法是将__builtin_expect指令封装为likely和unlikely宏。这两个宏的写法如下.

#define likely(x) __builtin_expect(!!(x), 1) //x很可能为真       
#define unlikely(x) __builtin_expect(!!(x), 0) //x很可能为假

(3)示例

int x, y;
 if(unlikely(x > 0))
    y = 1; 
else 
    y = -1;

上面的代码中 gcc 编译的指令会预先读取 y = -1 这条指令,因为你告诉编译器 x > 0的可能性很小。
如果x > 0可能性很大,就该用 likely(x > 0),这样编译出的指令是预先读取 y = 1 这条指令了。这样系统在运行时就会减少重新取指了。

总结:
unlikely()表示:不太可能为 真
likely()表示:很有可能为 真
代码逻辑没有改变,还是括号里的含义,提高运行效率,用于在if判断时优先判断哪个

3. 构造、attribute((constructor))

参考1
(1)概况
gcc为函数提供了几种类型的属性,其中包含:构造函数(constructors)和析构函数(destructors)。

(2)代码实例
#include<stdio.h>
attribute((constructor)) void before_main() {
printf(“Before main\n”);
}
attribute((destructor)) void after_main() {
printf(“After main\n”);
}
int main(int argc, char **argv) {
printf(“In main\n”);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值