C中函数调用的默认参数
假设在一个文件中,我在同一项目的两个文件中有一些代码,
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);
当从 file2.c 调用时,bool d 的值是多少?我知道这是非常糟糕的做法,但我正在维护旧代码并且有人这样做了,我只是想知道默认参数或者它是否依赖于实现。另请注意,此示例中的 bool
是软件的 typedef,因为该特定项目不支持 C99。谢谢。!
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? 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. Thank you.!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该值将是未定义的。调用 func1 时,其参数入栈。如果您使用少 1 个参数来调用它,堆栈将比进程期望的少
sizeof(bool)
字节。这不会使您的程序崩溃,因为您的堆栈和堆“面向”,但如果您尝试访问 d,您将访问堆栈上的任何值 -> 。垃圾。The value would be undefined. When calling func1, its parameters go on the stack. If you call it with 1 parameter less, the stack will be
sizeof(bool)
bytes short of what the process expects. This will not crash your program as your stack and your heap are "facing", but if you try to access tod
, you will access to whatever value is there on the stack -> rubbish.该值不仅取决于实现;而且取决于实现。程序的整个行为是未定义的。如果您将
func1
的声明放在标头中而不是file2.c
中,并且您会将该标头包含在file1.c
中,这是良好的 C 实践,编译器将拒绝编译它。在实践中,您可能会观察到
d
具有一些任意的、不可预测的值,尽管您的程序也可能神秘地崩溃。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 infile2.c
and you'd include that header infile1.c
, as is good C practice, the compiler would refuse to compile this.In practice, you'll likely observe
d
as having some arbitrary, unpredictable value, though your program might also crash mysteriously.该程序是未定义的行为。由于程序是未定义的行为,编译器有权拒绝编译它。
程序中的两个函数声明不兼容;它们没有相同数量的参数。
This program is undefined behavior. As the program is undefined behavior the compiler has the right to refuse to compile it.
The two function declarations in your program are not compatible; they don't have the same number of parameters.
这是未定义的行为 - 它可能具有垃圾值,也可能崩溃,具体取决于编译器和操作系统的调用约定。
编辑:如果其他参数从左到右推,它们也可能是混合的。
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.
它可以是任何值,我相信如果您以这种方式调用该方法,则可以是堆栈中的垃圾。
该程序将具有未定义的行为,因为您确实不知道
bool
参数的值。执行过程中也可能崩溃。希望有帮助。
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. It may also crash during the execution.Hope it helps.