简体   繁体   English

gcc没有警告调用带有参数的nullary函数?

[英]gcc doesn't warn about calling a nullary function WITH parameters?

Can anyone tell me, why in the blazes GCC (eg 4.4.3) does not warn about or error the incorrect call of a nullary function? 任何人都可以告诉我,为什么在大火中GCC(例如4.4.3)没有警告或错误的错误调用Nullary函数?

void hello() {
}

int main( int argc, char* argv[] ) {
 int test = 1234;
 hello(test);

 return 0;
}

(see also http://bytes.com/topic/c/answers/893146-gcc-doesnt-warn-about-calling-nullary-function-parameters ) (另见http://bytes.com/topic/c/answers/893146-gcc-doesnt-warn-about-calling-nullary-function-parameters

Because: 因为:

void hello() {

does not mean what you think it does. 并不意味着你的想法。 Use: 使用:

void hello( void ) {

Without the void, you are saying you can't be bothered to specify the parameters. 没有虚空,你说你不能打扰指定参数。 Note this is one of the many ways that C differs from C++. 请注意,这是C与C ++不同的众多方式之一。

In C, void hello() declares a function hello() that returns a void and takes unspecified number of arguments. 在C中, void hello()声明一个函数hello() ,它返回一个void并获取unspecified number of arguments.

Note 注意

In C++ its all together a different scenario. 在C ++中,它们共同构成了一个不同的场景。 void hello() in C++ declares a function hello() that returns a void and takes no arguments. C ++中的void hello()声明一个函数hello() ,它返回一个void并且no arguments.

From what I can gather from The C Book 4.2 your function definition is not a prototype since it specifies no type information for the arguments. 根据我从C Book 4.2中收集的内容,您的函数定义不是原型,因为它没有为参数指定类型信息。 This means the compiler only remembers the return type and retains no information on the arguments whatsoever. 这意味着编译器只记住返回类型,并且不保留任何关于参数的信息。

This form of definition is still allowed for backward compatibilty and is not restricted to functions that take no arguments. 这种形式的定义仍然允许向后兼容,并且不限于不带参数的函数。 gcc will equally allow something like gcc同样会允许类似的东西

void hello( a ) {
}

int main( int argc, char* argv[] ) {
 int test = 1234;
 hello(test,1);

 return 0;
}

It is only the lack of type information for the arguments that is important here. 这里只有缺少类型信息的参数才是重要的。 To fix this and ensure that gcc checks the arguments when the function is used you can put the type information in either a declaration of your function or the definition. 要解决此问题并确保gcc在使用函数时检查参数,您可以将类型信息放在函数声明或定义中。 Preferably you would put them in both. 你最好把它们放在两者中。

All of this still doesn't really answer your question of course as to why gcc doesn't warn you. 所有这些仍然没有真正回答你的问题,为什么gcc没有警告你。 It must be the case that the gcc team feel there is still enough old-style C code out there to justify suppressing the warning by default. 必须是gcc团队认为仍有足够的旧式C代码来证明默认情况下抑制警告的情况。 IMO I'm surprised that the -Wstrict-prototype option as mentioned by @caf is not on by default. IMO我很惊讶-Wstrict-prototype提到的-Wstrict-prototype选项默认不启用。

Ha, I had this the other day. 哈,前几天我有这个。

Your definition needs to be: 您的定义必须是:

void hello(void);

Else the function can accept any number of parameters. 否则该函数可以接受任意数量的参数。

But I do understand your point. 但我明白你的观点。 There is almost no compilers that even give the slightest warning for it. 几乎没有任何编译器甚至会对它提出丝毫警告。

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

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