Joe Wright <> writes:
> Szabolcs Nagy wrote:
>> i'm writing because i wonder about the semantics of overridden
>> standard functions
>>
>> i imagined that when i compiled a c code then a linker would link my
>> code against a libc so there would be errors about multiple
>> definitions of overridden standard functions, however
>>
>> #include <stdio.h>
>>
>> int fclose(FILE *f) {
>> puts("overridden fclose");
>> return 0;
>> }
>>
>> int main(void) {
>> return fclose(stdout);
>> }
>>
>> compiles cleanly with the compiler implementations i've tried
>>
>> does the standard allow this?
>>
>> what should happen if i compile (and link) this code together with
>> other code that uses fclose()? (i guess fclose should not be
>> implemented by a macro in the standard lib so the overridden fclose
>> would be used there)
>>
> Nothing to do with C really. If a definition of fclose() exists in the
> .c file then it is 'resolved' there and we never ask the linker to
> find it for us.
Nevertheless, the behavior is undefined, and the implementation may
legally do anything it likes with the call, including calling the
user-defined function, calling the library function, or anything
else.
For the code shown, if <stdio.h> defines fclose() as a function-like
macro, then the function declaration is almost certainly a syntax
error. You can avoid that particular problem with "#undef fclose",
but the behavior is still undefined.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"