Why does C carry over missed function declarations?

Today we are faced with an unusual phenomenon, a colleague called a normally functioning function in his code that called segfault in libc (gethostbyname). The mysterious thing was that the same function worked without problems in other source files at the same runtime. Surprisingly, segfault disappeared when valgrind was used, in fact it worked perfectly with valgrind, without errors.

After many sacrifices, in order to reassure the gods of the compiler, we finally realized that the header file declaring the function was not in the source file that called this function. Once we added this, everything works fine.

Why does gcc / ld not generate an error (or even a warning) indicating that the function was not recognized ?, why does it work with valgrind?

Thank.

+2
source share
1 answer

Since you did not use the correct warning options, for example -Wall -Wmissing-prototypes -Wstrict-prototypes. By default, gcc is pretty liberal in what it accepts. The C language (at least C89) has the concept of implicit function declarations, where a function without a prototype has a return type and an argument list obtained from its first use in a function call, and this is not enough, it returns an int and takes an indefinite but fixed amount arguments (i.e. cannot be a vararg function).

+8
source

All Articles