功能隐式声明 [英] implicit declaration of function

查看:174
本文介绍了功能隐式声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC通常产生该警告时,不包含正确的头文件中。此链接 - > www.network-theory.co.uk/docs/ gccintro / gccintro_19.html 说,由于函数声明是隐式的(而不是通过一个头显式声明)了错误的参数类型实际上可以传递给函数,产生不正确的结果。我不明白这一点。这是否意味着,编译器生成code,它推的东西,机器的字大小,压入堆栈,被叫方消费,并希望最好?

GCC typically yields this warning when the proper header file is not included. This link --> www.network-theory.co.uk/docs/gccintro/gccintro_19.html says that because the function declaration is implicit (rather than explicitly declared via a header) the wrong argument types could actually be passed to the function, yielding incorrect results. I don't understand this. Does this mean the compiler generates code that pushes something, of the machine's word size, onto the stack for the callee to consume, and hopes for the best?

详细为AP preciated。

Detail is appreciated.

推荐答案

如果编译器不会对参数应如何通过具体的信息,比如当有没有原型或者用于传递的参数,其中原型有省略号('...'),编译如下传递的参数一定的规则。这些规则基本上遵循什么发生在pre-标准(或K&安培; R)C - 用于原型之前。从C99 6.5.2.2/6转述函数调用:

If the compiler doesn't have specific information about how the argument should be passed, such as when there's no prototype or for arguments that are passed where the prototype have an ellipsis ('...'), the compiler follows certain rules for passing the arguments. These rule basically follow what occurred in pre-standard (or K&R) C - before prototypes were used. Paraphrased from C99 6.5.2.2/6 "Function calls":

* the integer promotions are applied
* if the argument has float type it's promoted to double

应用这些默认参数提升后,参数简单地复制到哪里编译器通常拷贝参数(一般,堆栈)。因此,一个结构参数将被复制到堆栈中。

After these default argument promotions are applied, the argument is simply copied to wherever the compiler normally copies arguments (generally, the stack). So a struct argument would be copied to the stack.

如果实际的功能实现不匹配的编译器是如何创建的参数,那么你会得到未定义行为(与符号/无符号不匹配,如果异常值可以重新presented或指针到char和指针作废可混合/匹配)。

If the actual function implementation doesn't match how the compiler creates the parameters, then you get undefined behavior (with exceptions for signed/unsigned mismatch if the value can be represented or pointers to char and pointers to void can be mixed/matched).

此外,在C90,如果函数是隐式声明(其中C99不允许,但它确实允许功能,而无需原型),返回值默认为 INT 。再次,实际的函数返回别的东西,不确定的行为。

Also in C90, if the function is implicitly declared (which C99 doesn't permit, though it does permit functions without prototypes), the return value is defaulted as int. Once again, the the actual function returns something else, undefined behavior results.

这篇关于功能隐式声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆