C $ C $的C大括号的使用目的在Linux中发现(在include / linux / list.h)? [英] Purpose of Curly Brace Usage of C Code found in Linux (include/linux/list.h)?

查看:386
本文介绍了C $ C $的C大括号的使用目的在Linux中发现(在include / linux / list.h)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到下面的code在Linux中来(在include / linux / list.h)。我感到困惑线713特别,我不明白({N = POS-> member.next; 1;})。

什么是花括号做什么?为什么会出现1在此声明?

如果有人可以解释这一行这将是更AP preciated。请注意,我不需要的链接列表和#define语句是如何工作的解释,等等。

  704 / **
705 * hlist_for_each_entry_safe - 对去除列表项的安全特定类型的迭代名单
706 * @pos:类型*为循环使用的光标。
707 * @n:另一个与放大器;结构hlist_node用作临时存储
708 * @head:头部为你的列表中。
709 * @member:结构内的hlist_node的名称。
710 * /
711的#define hlist_for_each_entry_safe(POS,N,头,成员)\\
712(POS = hlist_entry_safe((头) - >首先,typeof运算(* POS),成员); \\
713 POS和放大器;&安培; ({N = POS-GT&; member.next; 1;}); \\
714 POS = hlist_entry_safe(N的typeof(* POS),成员))
715


解决方案

这是一个的语句前pression 的。它是一个<一href=\"http://stackoverflow.com/questions/3079721/are-statement-and-declarations-in-ex$p$pssions-specific-to-gnu-c\">gcc根据文档并href=\"http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html\" rel=\"nofollow\"> 6.1语句和声明防爆pressions 的:


  

在复合语句的最后一件事应该是一个前pression跟一个分号;此SUBEX pression的值作为整个构建体的值


其中在的情况下,为code:

 ({N = POS-GT&; member.next; 1;})

值将是 1 。根据文档:


  

这个功能是使宏定义是特别有用的安全(让他们评估每个操作数恰好一次)。


这给出了这样的例子,而无需使用的语句前pressions 的:

 的#define MAX(A,B)((a)及GT;(二)(一):(B))

与此的安全的版本,与你知道操作数的类型的警告:

 的#define MAXINT(A,B)\\
   ({INT _a =(a)中,_b =(二); _a&GT; _b _a:_b;})

这是众多 GCC的扩展一>

I came across the following code within Linux (include/linux/list.h). I'm confused about line 713. In particular, I don't understand ({ n = pos->member.next; 1; }).

What is the curly braces doing? Why is there a '1' in this statement?

If someone could explain this particular line it would be much appreciated. Note, I don't need an explanation of how link lists and #defines work, etc.

704 /**
705  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
706  * @pos:        the type * to use as a loop cursor.
707  * @n:          another &struct hlist_node to use as temporary storage
708  * @head:       the head for your list.
709  * @member:     the name of the hlist_node within the struct.
710  */
711 #define hlist_for_each_entry_safe(pos, n, head, member)                 \
712         for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
713              pos && ({ n = pos->member.next; 1; });                     \
714              pos = hlist_entry_safe(n, typeof(*pos), member))
715 

解决方案

This is a statement expression. It is a gcc extension and according to the documentation 6.1 Statements and Declarations in Expressions:

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.

Which in the case, for the code:

({ n = pos->member.next; 1; })

the value would be 1. According to the documentation:

This feature is especially useful in making macro definitions "safe" (so that they evaluate each operand exactly once).

It gives this example without using statement expressions:

#define max(a,b) ((a) > (b) ? (a) : (b))

versus this safe version, with the caveat that you know the type of the operands:

#define maxint(a,b) \
   ({int _a = (a), _b = (b); _a > _b ? _a : _b; })

This is one of the many gcc extensions used in the Linux kernel.

这篇关于C $ C $的C大括号的使用目的在Linux中发现(在include / linux / list.h)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆