在Linux中使用C代码的Curly Brace用途(includelinuxlist.h)?

在Linux中使用C代码的Curly Brace用途(includelinuxlist.h)?,第1张

概述我在 Linux中遇到了以下代码(include / linux / list.h).我对第713行感到困惑.特别是,我不明白({n = pos-> member.next; 1;}). 花括号做什么?为什么这个陈述中有’1’? 如果有人能解释这一特定的行,我将不胜感激.注意,我不需要解释链接列表和#defines如何工作等. 704 /**705 * hlist_for_each_entry 我在 Linux中遇到了以下代码(include / linux / List.h).我对第713行感到困惑.特别是,我不明白({n = pos-> member.next; 1;}).

花括号做什么?为什么这个陈述中有’1’?

如果有人能解释这一特定的行,我将不胜感激.注意,我不需要解释链接列表和#defines如何工作等.

704 /**705  * hList_for_each_entry_safe - iterate over List of given type safe against removal of List entry706  * @pos:        the type * to use as a loop cursor.707  * @n:          another &struct hList_node to use as temporary storage708  * @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)                 2         for (pos = hList_entry_safe((head)->first,typeof(*pos),member);3              pos && ({ n = pos->member.next; 1; });                     4              pos = hList_entry_safe(n,member))715
解决方法 这是一个声明表达式.它是 gcc extension并且根据文档 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.

在这种情况下,对于代码:

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

值为1.根据文档:

This feature is especially useful in making macro de@R_502_4654@ns “safe” (so that they evaluate each operand exactly once).

它给出了这个例子而不使用语句表达式:

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

与这个安全版本相比,您需要知道 *** 作数的类型:

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

这是众多gcc extensions used in the Linux kernel中的一个.

总结

以上是内存溢出为你收集整理的在Linux中使用C代码的Curly Brace用途(include / linux / list.h)?全部内容,希望文章能够帮你解决在Linux中使用C代码的Curly Brace用途(include / linux / list.h)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1023507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存