码迷,mamicode.com
首页 > 其他好文 > 详细

复合文字(Compound Literals)

时间:2019-12-14 22:57:14      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:etc   test   gcc   const   数组   params   编译   x86   亦或   

复合文字(Compound Literals)

阅读代码时发现了这行

1
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&(int){1},sizeof(int));

发现之前没有见过这种写法

1
&(int){1}

上网搜索发现Compound Literalsc99添加的新特性,gccc90c++中也支持此种特性,但在c++中的语义有所不同.

官网上举的例子是:

假如一个结构体的定义如下:

1
2
3
4
struct {
int a;
char b[2];
}structure;

那么使用复合文字构造一个结构体的方法如下:

1
structure = ((struct foo){x + y,'a',0});

这相当于:

1
2
3
4
{
struct temp = {x + y, 'a', 0};
structure = temp;
}

验证下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


struct {
int a;
char b[2];
}structure;

int main()
{
int x = 1;
int y = 2;
structure = ((struct foo){x + y,'a',0});
printf("stu.a:%dn",structure.a);
printf("stu.b:%sn",structure.b);
return 0;
}

在我的系统上:

大专栏  复合文字(Compound Literals)">
1
2
3
4
5
6
[test-dir] uname -r
3.10.0-693.17.1.el7.x86_64
[test-dir] gcc -version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
[test-dir] cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

编译通过:

1
gcc test.c -o test

运行:

1
2
3
[test-dir] ./test                                                     
stu.a:3
stu.b:a

还可以用来初始化二维数组:

1
char **foo = (char *[]) { "x", "y", "z" };

变量:

1
int i = ++(int) { 1 };

作为GNU的拓展,gcc允许通过这用特性初始化静态变量

1
2
3
static struct  x = (struct ) {1, 'a', 'b'};
static int y[] = (int []) {1, 2, 3};
static int z[] = (int [3]) {1};

相当于:

1
2
3
static struct foo x = {1, 'a', 'b'};
static int y[] = {1, 2, 3};
static int z[] = {1, 0, 0};

在C中,复合文字指定具有静态或自动存储时间的未命名对象,而在C++中代表一个临时的对象,生命周期只能到其表达式的末尾。结果就是在C中复合文字中的子对象会被分配地址,在C++中则是未定义的行为,因此g++不会为临时的数组转化为指针。

例如,如果数组复合文字foo的实例出现在函数内部,在C++中后续任何使用foo的结果都是未定义的,因为数组foo的生命周期在其声明之后就结束了。

作为优化,在g++中有时会给复合文字数组更长的生命周期,例如在函数之外或者用const修饰,如果foo及其初始化程序具有char * const类型而不是char *类型的元素,亦或者foo是全局变量,这个数组则有静态存储的生命周期,但是,避免在C++代码中使用复合文字是最安全的。

复合文字(Compound Literals)

标签:etc   test   gcc   const   数组   params   编译   x86   亦或   

原文地址:https://www.cnblogs.com/lijianming180/p/12041284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!