简体   繁体   English

C中函数调用中的默认参数

[英]default parameters in function call in C

Lets say in a file I have some code in two files part of the same project 让我们说在一个文件中,我在同一个项目的两个文件中有一些代码

file1.c
int func1(int a, int b, int c, bool d)
{
        /* function body */
}

file2.c
extern func1(int a, int b, int c);

/* function call */
func1(runtime1, runtime2, runtime3);

What would be value bool d takes when being called from file2.c? 从file2.c调用时bool d会有什么价值? I know this is really bad practice, but I am maintaining old code and somebody did this, I just want to know the default parameter or if it is implementation dependent. 我知道这是非常糟糕的做法,但是我维护旧代码并且有人这样做了,我只是想知道默认参数或者它是否依赖于实现。 Please note also, that bool in this example is a typedef of the software, since this specific project does not support C99. 另请注意,此示例中的bool是软件的typedef,因为此特定项目不支持C99。 Thank you.! 谢谢。!

The value is not just implementation-dependent; 价值不仅仅取决于实施; the entire behavior of the program is undefined. 程序的整个行为是未定义的。 If you'd put the declaration for func1 in a header instead of in file2.c and you'd include that header in file1.c , as is good C practice, the compiler would refuse to compile this. 如果你把func1的声明放在一个头而不是file2.c并且你在file1.c包含那个头,那么好的C实践,编译器会拒绝编译它。

In practice, you'll likely observe d as having some arbitrary, unpredictable value, though your program might also crash mysteriously. 在实践中,您可能会观察到d具有一些任意的,不可预测的值,尽管您的程序可能也会神秘地崩溃。

The value would be undefined. 该值将是未定义的。 When calling func1, its parameters go on the stack. 调用func1时,其参数将进入堆栈。 If you call it with 1 parameter less, the stack will be sizeof(bool) bytes short of what the process expects. 如果用少参数调用它,则堆栈的sizeof(bool)字节将低于进程所期望的值。 This will not crash your program as your stack and your heap are "facing", but if you try to access to d , you will access to whatever value is there on the stack -> rubbish. 这不会使您的程序崩溃,因为您的堆栈和堆正在“面对”,但如果您尝试访问d ,您将访问堆栈中的任何值 - >垃圾。

This program is undefined behavior . 该程序是未定义的行为 As the program is undefined behavior the compiler has the right to refuse to compile it. 由于程序是未定义的行为,编译器有权拒绝编译它。

(C99, 6.2.7p2) "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined." (C99,6.2.7p2)“所有引用相同对象或函数的声明都应具有兼容类型;否则,行为未定义。”

The two function declarations in your program are not compatible; 程序中的两个函数声明不兼容; they don't have the same number of parameters. 它们没有相同数量的参数。

(C99, 6.7.5.3p15) "For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types." (C99,6.7.5.3p15)“对于要兼容的两种函数类型,两者都应指定兼容的返回类型。此外,参数类型列表(如果两者都存在)应在参数数量和省略号终止符的使用中一致;相应的参数应具有兼容的类型。“

It can be any value, garbage from the stack I believe if you call the method that way. 它可以是任何值,来自堆栈的垃圾我相信如果你这样调用方法。

The program will have undefined behavior because you really do not know the value of the bool parameter. 程序将具有未定义的行为,因为您实际上不知道bool参数的值。 It may also crash during the execution. 它也可能在执行期间崩溃。

Hope it helps. 希望能帮助到你。

It's undefined behavior - it may have garbage value, and it may also crash, depend on the calling convention of your compiler and OS. 它是未定义的行为 - 它可能有垃圾值,它也可能崩溃,取决于编译器和操作系统的调用约定。

Edit: The other arguments may also be mixed, if they are been pushed from left to right. 编辑:如果从左到右推送其他参数,则其他参数也可以混合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM