Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > overriding standard functions

Reply
Thread Tools

overriding standard functions

 
 
Szabolcs Nagy
Guest
Posts: n/a
 
      09-13-2009
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)

 
Reply With Quote
 
 
 
 
krishan
Guest
Posts: n/a
 
      09-13-2009
On Sep 13, 6:37*pm, Szabolcs Nagy <(E-Mail Removed)> 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)


Why are you doing this? you can use LD_PRELOAD if you want to do this.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-13-2009
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?


The standard says that this is undefined behavior; a phrase that gives
implementations enough freedom to either allow it, disallow it, or have
it malfunction, among an infinity variety of more complicated options. I
know that there are systems which do allow it, and ones that don't - I'm
not sure how many of the other more complicated possibilities are
actually implemented.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-13-2009
On Sun, 13 Sep 2009 06:37:37 -0700 (PDT), Szabolcs Nagy
<(E-Mail Removed)> 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)


As others have noted, you have gone beyond the standard and if you
need a definitive answer for a particular implementation, you need to
ask in a group where experts on that implementation live.

On all the systems I have worked on (a statistically insignificantly
small sample so take with a grain of salt), the linkers have always
used "for an external reference that requires definition, use the
first definition encountered in the specified search order" rule.

In your example, since your object file is obviously first, it
would use one you defined and ignore any other definition of fclose
that it found (e.g., while looking for the definition of puts).

If you had split the two functions into separate source files
and somehow directed the linker to search main, system, and fclose
object files in that order, then it would have used the system
function and ignored yours.

However, if an object file defines multiple external entities, when
that object file is included, all the entities become known to the
linker. If two such entities in different included object files have
the same name, that is when the linker complains about duplication.
For example:

main.c
int mine = 5;
void func2(void); /*prototype*/
void func1(void){...}
int main(void){
...
func1();
...
func2();
...}

func2.c
int mine = 10;
void func1(void){...}
void func2(void){...}

In the process of resolving references from main.c's object file, it
must include func2.c's object file. At that point it should complain
about mine and func1 both being multiply defined.

--
Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-13-2009
Joe Wright <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      09-14-2009
On Sun, 13 Sep 2009 06:37:37 -0700, Szabolcs Nagy wrote:

> i'm writing because i wonder about the semantics of overridden
> standard functions


> what should happen if i compile (and link) this code together with
> other code that uses fclose()?


This is extremely implementation-specific. If it doesn't simply result
in an error, your program may use either your version or libc's
version, libc may use either your version or libc's version, and other
libraries may use either your version or libc's version. Most of the
combinations are possible (although your program using libc's version
while everything else uses your version is pretty unlikely).

If you only want your program (and not third-party libraries) to use your
version, give your function a different name and define e.g. fclose as a
macro.

If you want to force libraries to use your version, you'll need to
read the documentation for your linker, loader, and the platform's
executable format (ELF, COFF etc).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
Is overriding a function of a library in accordance with C++ standard? Lighter C++ 7 08-25-2006 06:01 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
overriding static and virtual members with same proto. What is the standard ? Philippe Guglielmetti C++ 3 10-22-2003 05:00 PM
Overriding virtual functions problem AGoTH C++ 7 08-09-2003 11:22 PM



Advertisments