开发者

How does the warning of implicit declaration of any function affect my code ?

See I have made a library which has several .h & several .c files.

Now under some circumstances I have not removed a warning that says

warning: implicit declaration of function ‘getHandle’

开发者_Python百科Now I want to ask you, does this cause any problem in the binary of my library?

Will it negatively affect the execution of my library on embedded platforms or anywhere else?


In C90, a call to a function with no visible declaration creates an implicit declaration of a function returning int and taking the promoted types of the arguments. If your getHandle function returns a pointer, for example, then the compiler will generate code assuming that it returns an int. Assigning the result to a pointer object should at least trigger another warning. It might work ok (if int and the pointer type are the same size, and int and pointer function results are returned in the same way), or it could go badly wrong (if, for example, int is 32 bits and pointers are 64 bits, or if pointer results are returned in 68K-style address registers).

The C99 standard removed implicit int from the language. Calling a function with no visible declaration is a constraint violation, requiring a diagnostic and possibly causing your program to be rejected.

And if you just fix the problem, you don't have to waste time figuring out how and whether it will work if you don't fix it.


In such a case the compiler cannot verify that the usage of getHandle is proper - that is, whether the return value and the arguments are of the right type and count. It will just accept the way you call it without any explicit statement that this is the right way.

Adding a prototype of the function is a way to tell the compiler that this is the intended usage of the function and it will be a compile-time error not to comply to that.

It may cause some nasty bugs in case there is a mismatch between the way it's called and the way the function expects its arguments. The most likely effect will be a corruption of the stack.

If there isn't a mismatch, it won't make any difference at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜