We came across an unusual phenomenon today, a colleague was calling a normally well-behaved function in his code which was triggering a segfault in libc (gethostbyname). The puzzling thing was that the same function worked without trouble in other source files in the same run-time. Astoundingly, the segfault disappeared when valgrind was used, in fact, it worked perfectly with valgrind, with no errors reported.
After much sacrifices to appease the compiler gods we eventually realised that the header file declaring the function was missing from the source file which called the function. Once we added this in, everything ran normally.
Why did gcc/ld not generate an error (or even a warning) indicating that the function was not recognised?, why did it work with valgrind?
Thanks.