如果我不包含标题,为什么在调用函数之前清除EAX?
在以下 C 代码中:
#include <stdio.h>
int main(void){getchar();}
它产生以下汇编:
main:
push rbp
mov rbp, rsp
# no extra instruction here when header is included
call getchar
mov eax, 0
pop rbp
ret
但是,如果我不包含stdio.h在文件中,那么它仍然可以编译,但会添加看起来像随机mov eax, 0指令的内容:
这是编译器资源管理器:https : //godbolt.org/z/3fTcss。这只是“未定义行为”的一部分,还是有特殊原因将之前的指令call getchar添加到那里?
回答
如果没有标头,gcc 会提供一个隐式声明,说明getchar您何时使用它,就好像您之前已经声明过一样
int getchar();
(此行为由旧版本的 C 标准保证。当前版本使使用先前未声明的函数成为未定义行为,但 gcc 仍提供旧行为作为扩展。)
此声明不提供有关getchar预期参数类型的信息。(请记住,与 C++ 不同,声明 with()不会将函数声明为不带参数,而是带未指定的参数,让程序员知道函数期望什么并传递正确数量和类型的参数。)就编译器所知,它甚至可能是可变参数,并且根据 x86-64 SysV ABI,可变参数函数期望al用于传递参数的向量寄存器的数量。这里没有使用向量寄存器,所以编译器在调用之前设置al为0。(实际上将所有内容归零会更有效,rax因此它会这样做。)
- The C standard from the very beginning said that implicitly declared functions and those declared without a prototype could not be variadic. But most compilers support it anyway.
- @Joshua: The first C standard (C89) did. You're right that earlier pre-standard C did not.
- Current versions don't make use of undeclared functions UB. The implementation must emit a diagnostic, and may either stop compilation, or implicitly define the function and continue as it would in C89. I didn't look in the Standard itself, but at least the C99 Rationale document is quoted [here](https://stackoverflow.com/a/17937964/673852).