- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在查看 glibc 的一些源代码,特别是 nptl 代码,我发现它有点难以理解,因为它似乎有我不熟悉的约定。
例如,我正在查看一个非常小的文件 pthread_equal.c有几件事我有疑问:
22 int
23 __pthread_equal (thread1, thread2)
24 pthread_t thread1;
25 pthread_t thread2;
26 {
27 return thread1 == thread2;
28 }
29 strong_alias (__pthread_equal, pthread_equal)
第 22 行和第 23 行的声明看起来像我理解的内容。它有一个返回类型 int
然后是函数名 __pthread_equal
和参数列表 (thread1, thread2)
。但是第 24 行 pthread_t thread1;
和第 25 行 pthread_t thread2;
的声明是干什么用的?看起来这些被声明为全局变量,但我不明白其目的。我在 nptl 目录中的许多文件中都看到过这种模式,但一直无法弄清楚为什么会这样。
什么是strong_alias
?快速 Google 搜索有使用此方法的示例,但我没有找到指向任何文档的链接。
为什么要在某些名称前加上两个下划线 __
而在某些名称前加上一个下划线 _
?我见过的大多数代码都使用了两个下划线,但我认为我已经看到一些地方使用了一个下划线。例如在 pthreadP.h
556 /* Old cleanup interfaces, still used in libc.so. */
557 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
558 void (*routine) (void *), void *arg);
559 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
560 int execute);
561 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
562 void (*routine) (void *), void *arg);
563 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
564 int execute);
不可否认,该代码以“旧清理接口(interface)”的注释开头,但无论哪种方式,我都很好奇其中的区别以及为什么有时使用一个下划线,有时使用两个下划线。
欢迎提供有关这些问题的任何信息。
最佳答案
该函数是在不需要 C89 兼容编译器的情况下编写的;它也适用于旧的编译器。那是一个非原型(prototype)函数定义。
int /* Return type */
function(arg1, arg2) /* Function name and argument names (but no types) */
int arg1; /* Type of arg1 */
char *arg2; /* Type of arg2 */
{
/* Body of function */
}
请注意,参数的定义不必与函数行中的顺序相同(我不得不将代码从这种“K&R”表示法转换为原型(prototype)表示法,因为它们是乱序的!)。另请注意,过去可以简单地编写:
main(argc, argv)
char **argv;
{
...
}
argc
的隐含类型是 int
,因为它没有被指定为任何其他类型。 glib
代码不太可能利用该许可证。同样,main()
的返回类型是 int
,因为没有给出其他类型。
strong_alias
与隐藏和公开共享库中的符号有关。我没有使用它,所以我不确定所有的后果,但我相信这意味着 __pthread_equal()
是 pthread_equal()
函数的另一个名称.
__pthread_equal()
名称背后的部分原因是,以下划线开头后跟大写字母或另一个下划线的名称由 C 标准“保留给实现”。根据 C 标准,“pthread_equal()”等名称位于用户的 namespace 中。
ISO/IEC 9899:1990(C99 标准)说:
7.1.3 Reserved identifiers
Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.
— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).
— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154)
— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.
154) The list of reserved identifiers with external linkage includes
errno
,math_errhandling
,setjmp
, andva_end
.
关于c - 了解 glibc 源代码约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695182/
假设我有一个函数,例如 f(x,y)但y参数是可选的。设置y的首选方式是什么?作为可选参数?对我有用的一种选择: function f(x, y=nothing) # do stuff
在学习核心动画时,我很快就了解到,如果你做得不对,你会得到非常奇怪的未定义行为。为此,我有几个问题可以帮助我从概念上更好地理解它。 我的 NSView 子类在其 init.h 中声明以下内容:该 Vi
我一直在尝试学习 ClojureScript,并偶然发现了一些非常神秘的函数名称。 例如: (.-length str) 来自 om 文档: (defn add-contact [data owner
我对此很好奇。我最近想到使用大括号来隔离代码段,以进行可视化组织,并将变量隔离到特定范围(如果只是为了防止它们在较大的函数中混淆 Eclipse 中的建议)。例如: public void start
所以我进行了一些搜索,但在 Google 或 PEP 上找不到任何讨论此问题的内容。 我正在使用 tkinter 做一个项目,我有一个文件,它是项目的一部分,只有 200 行代码(不包括所有注释掉的代
根据某种不成文的约定,所有 API key 都是十六进制数字吗? 最佳答案 对一些半随机数据(例如时间戳 + 用户 ID + key )进行 md5 哈希是一种快速生成难以猜测的固定长度 key 的方
在 C 中,如果编译器想要提供实现定义的标识符(语言扩展、内在函数、伪函数和伪宏,基本上任何不是语言标准保留关键字但也不是常规函数的东西)惯例是名称以下划线为前缀;据了解,代码不应出于正常目的使用此类
Java Bean 的约定之一是: setter 的返回类型必须是 void。 或者至少大多数人是这么说的。我的问题是:它真的必须无效吗?我喜欢返回 "this" 而不是 "void" 因为我可以像这
我对 Java 编码风格有疑问。 我编码: public class Pattern { public Pattern(...) { ... } public static List
我有一个关于 Java servlet 约定的问题。在查看 servlet 的任何教程时,无论是 Eclipse、NetBeans 等,它们总是让您创建一个 index.jsp 页面。创建页面后,他们
在 python 中,当变量名与保留字冲突时(如 class、in、default 等),PEP8约定规定应使用尾随下划线(class_、in_、default_)。 对于相同的情况,共享最多的 ja
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
Java 存储预定义字符串集的主要约定是什么?现在我有一个包含我使用的所有字符串的类,但感觉确实有更好的方法来做到这一点。 这就是我在 my_strings.java 类中所做的 public fin
我经常需要检索结果并按给定列访问它们。 有没有一种方法可以不用每次都遍历整个数据集来编写它? 我研究了各种 PDO 获取模式,但没有发现比这更简单的模式。谢谢。 function get_groups
所以我正在制作这个 android 应用程序,它需要从用户提供的 CSV 文件中读取数据。 CSV 文件在台式电脑上编辑起来更舒服,所以我在应用程序中没有编辑器,它是“只读”的;我假设手机的 SD 卡
我正在编写一个文件,该文件是 npycurses module 的子类 npycurses.ActionFormV2 ,我正在覆盖 beforeEditing method ,所以当我整理我的文件时,
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在开发一个可在 iPhone、iPad 和其他移动设备上运行的网页。我很好奇是否有关于移动设备 CSS 最佳实践的资源。 我试过搜索,但想出了随机网站 with tidbits of inform
我发现开始编程时最大的挑战之一是掌握文件路径。例如,当您引用目录结构中同一级别的文件夹中的路径时,您将看到: /folder/style.css 或 文件夹/style.css等 当您引用不同的文件时
如果我有如下函数: def foo(): return 1, 2 我通常会这样调用函数: g, f = foo() 但是,如果我从不打算使用返回的第二个值,是否有一种调用函数的方法可以明确说明
我是一名优秀的程序员,十分优秀!