C中函数调用的默认参数

发布于 2025-01-03 19:38:21 字数 391 浏览 1 评论 0原文

假设在一个文件中,我在同一项目的两个文件中有一些代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妞丶爷亲个 2025-01-10 19:38:21

该值将是未定义的。调用 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 to d, you will access to whatever value is there on the stack -> rubbish.

云胡 2025-01-10 19:38:21

该值不仅取决于实现;而且取决于实现。程序的整个行为是未定义的。如果您将 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 in file2.c and you'd include that header in file1.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.

不气馁 2025-01-10 19:38:21

该程序是未定义的行为。由于程序是未定义的行为,编译器有权拒绝编译它。

(C99, 6.2.7p2) “引用同一对象或函数的所有声明应具有兼容的类型;否则,行为未定义。”

程序中的两个函数声明不兼容;它们没有相同数量的参数。

(C99, 6.7.5.3p15) “为了使两个函数类型兼容,两者都应指定兼容的返回类型。此外,参数类型列表(如果两者都存在)应在参数数量和使用方面一致省略号终止符;相应的参数应具有兼容的类型。”

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."

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."

尛丟丟 2025-01-10 19:38:21

这是未定义的行为 - 它可能具有垃圾值,也可能崩溃,具体取决于编译器和操作系统的调用约定。

编辑:如果其他参数从左到右推,它们也可能是混合的。

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.

蓦然回首 2025-01-10 19:38:21

它可以是任何值,我相信如果您以这种方式调用该方法,则可以是堆栈中的垃圾。

该程序将具有未定义的行为,因为您确实不知道 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文