具有不完整声明的C函数 [英] C function with incomplete declaration
问题描述
我可以使用 gcc -std = C99 -Wall
来构建没有错误的示例:
I can build this example with no errors with gcc -std=C99 -Wall
:
void dummy() {}
int main(void) {
dummy(1, 2, 3);
dummy(120, 144);
}
反汇编显示该函数确实被调用了两次:
The disassembly shows that the function is indeed called twice:
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $15, %edx
movl $14, %esi
movl $12, %edi
movl $0, %eax
call foo
movl $300, %esi
movl $200, %edi
movl $0, %eax
call foo
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
我承认该代码不应该存在,但是它是允许的,我想知道在哪种特殊情况下它会有用.
I admit that this code should not exist, but, it is allowed and I am wondering in which special case it would be useful.
有任何线索吗?
编辑
问题 calling-c-functions-with-too-many-arguments不能回答这个问题.它提供了有关如何使用变量的信息,但未提供示例,其中不完整的声明可能很有用.
The question calling-c-functions-with-too-many-arguments does not answer this question. It gives information about how to use varadic but it does not provide an example in which an incomplete declaration can be useful.
问题 func-vs-funcvoid-in-c99 也没有回答问题.它解释了不完整原型和完整原型之间的区别,这不是我的问题.
The question func-vs-funcvoid-in-c99 also does not answer the question. It explains the difference between incomplete and complete prototype which is not my question.
所以,看来我的问题还不够清楚,我将举另一个例子:
So, it seems my question is not clear enough and I am going to give another example:
让我们想象一下,我真的会利用不完整的声明来使用变量参数而不使用变量方法,因此我将示例编写为:
Let's imagine I would really leverage an incomplete declaration to use variable arguments without using the varadic method so I wrote my example as:
int main(void) {
dummy(1, 2, 3);
dummy(1, 2, 3, 4, 5, 6, 7, 8);
}
根据调用约定,第一个函数将使用CPU寄存器传递参数,而第二个函数将使用堆栈.
Accoding to the calling conventions, the first function will use the CPU registers to pass the parameters while the second call will use the stack.
现在在我的 dummy
函数中,我如何阅读这些争论并知道是否使用了堆栈?
Now in my dummy
function, how can I read these arguements and know whether or not the stack was used?