redefine a function with a same name

Discussion in 'C Programming' started by paytam@gmail.com, Jun 23, 2006.

  1. Guest

    hi all
    I want to redefine a function getchar() in header stdio.h ,but I don't
    know what should I do.
    , Jun 23, 2006
    #1
    1. Advertising

  2. Richard Bos Guest

    wrote:

    > I want to redefine a function getchar() in header stdio.h ,but I don't
    > know what should I do.


    You should ask in a newsgroup dedicated to your particular compiler
    suite, since in the general case the ISO C Standard declares this to
    cause undefined behaviour.

    Richard
    Richard Bos, Jun 23, 2006
    #2
    1. Advertising

  3. said:

    > hi all
    > I want to redefine a function getchar() in header stdio.h ,but I don't
    > know what should I do.


    Want something else instead. Redefining standard library functions is a bad
    idea.

    But if you just want to be able to use a different name for it, that's fine.
    Just do something like this:

    #include <stdio.h>

    #define paytamTriesToGetACharFromStdin getchar

    int main(void)
    {
    int ch = 0;
    while((ch = paytamTriesToGetACharFromStdin()) != EOF)
    {
    putchar(ch);
    }
    return 0;
    }


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jun 23, 2006
    #3
  4. Guest

    wrote:
    > hi all
    > I want to redefine a function getchar() in header stdio.h ,but I don't
    > know what should I do.


    open the header file stdio.h - /usr/include/stdio.h and change it to
    whatever u want.
    done.
    , Jun 23, 2006
    #4
  5. Chris Dollin Guest

    wrote:

    > hi all
    > I want to redefine a function getchar() in header stdio.h ,but I don't
    > know what should I do.


    Something else.

    [You can't do so portably, implementation-specific techniques are implementation
    specific and generally offtopical, and you haven't explained why you want to
    pull the rug out from under your victims' feet.]

    --
    Chris "look! a unicorn! (fx:hack) I mean /horse/, sorry!" Dollin
    "Who are you? What do you want?" /Babylon 5/
    Chris Dollin, Jun 23, 2006
    #5
  6. Chris Dollin Guest

    wrote:

    > wrote:
    >> hi all
    >> I want to redefine a function getchar() in header stdio.h ,but I don't
    >> know what should I do.

    >
    > open the header file stdio.h - /usr/include/stdio.h and change it to
    > whatever u want.
    > done.


    You will be.

    First, editing the header file doesn't redefine any functions, always
    supposing you can do it.

    Second, if the OP is J Random Unix User, they won't be able to edit
    /usr/include/stdio.h, because they won't have permission to do so.

    Third, if the OP is J Random RISC OS User, there /isn't/ a file
    called /usr/include/stdio.h, there isn't a directory called
    /usr/include, there isn't a directory called /usr, and there isn't
    a directory called /.

    Fourth, in any case the standard includes need not be "files" /at
    all/, so there may be nothing to edit except the compiler
    executable. The term "playing with fire" doesn't begin to cover
    it.

    Finally, even if you /can/ do any of this, it's very likely you
    shouldn't. `getchar` is supposed to do what `getchar` is supposed
    to do - and people use it to do that.

    --
    Chris "making 'finally' topical on c.l.c" Dollin
    "Who are you? What do you want?" /Babylon 5/
    Chris Dollin, Jun 23, 2006
    #6
  7. CBFalconer Guest

    wrote:
    > wrote:
    >>
    >> I want to redefine a function getchar() in header stdio.h ,but I
    >> don't know what should I do.

    >
    > open the header file stdio.h - /usr/include/stdio.h and change it
    > to whatever u want.


    This has to be one of the most imbecelic (and wrong) replies ever
    seen in c.l.c, not to mention the childish use of 'u'.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
    CBFalconer, Jun 23, 2006
    #7
  8. Richard Bos Guest

    Chris Dollin <> wrote:

    > wrote:
    >
    > > open the header file stdio.h - /usr/include/stdio.h and change it to
    > > whatever u want.
    > > done.

    >
    > You will be.
    >
    > First, editing the header file doesn't redefine any functions, always
    > supposing you can do it.
    >
    > Second, if the OP is J Random Unix User, they won't be able to edit
    > /usr/include/stdio.h, because they won't have permission to do so.
    >
    > Third, if the OP is J Random RISC OS User, there /isn't/ a file
    > called /usr/include/stdio.h, there isn't a directory called
    > /usr/include, there isn't a directory called /usr, and there isn't
    > a directory called /.
    >
    > Fourth, in any case the standard includes need not be "files" /at
    > all/, so there may be nothing to edit except the compiler
    > executable. The term "playing with fire" doesn't begin to cover
    > it.


    Fifth, if you try to do this on a multi-user system the sysadmin will
    start withdrawing your privileges with extreme prejudice. Try doing it
    twice and the privileges withdrawn may well include the right to draw
    breath.

    Richard
    Richard Bos, Jun 23, 2006
    #8
  9. goose Guest

    CBFalconer wrote:
    > wrote:
    > > wrote:
    > >>
    > >> I want to redefine a function getchar() in header stdio.h ,but I
    > >> don't know what should I do.

    > >
    > > open the header file stdio.h - /usr/include/stdio.h and change it
    > > to whatever u want.

    >
    > This has to be one of the most imbecelic (and wrong) replies ever
    > seen in c.l.c,


    Maybe so, but many have considered some of *my* postings a
    worthy contender for that hotly contested title :)

    Anyway, what's the accepted wisdom about doing the following?
    (Is it at least well defined?)

    -------------
    #include <stdio.h>

    int mygetchar (void)
    {
    printf ("My getchar\n");
    return getchar();
    }

    #define getchar() mygetchar()

    int main (void)
    {
    char c = getchar ();
    printf ("got char '%c'\n", c);
    return 0;
    }

    -------------


    goose
    goose, Jun 23, 2006
    #9
  10. On 2006-06-23, goose <> wrote:
    > CBFalconer wrote:
    >> wrote:
    >> > wrote:
    >> >>
    >> >> I want to redefine a function getchar() in header stdio.h ,but I
    >> >> don't know what should I do.
    >> >
    >> > open the header file stdio.h - /usr/include/stdio.h and change it
    >> > to whatever u want.

    >>
    >> This has to be one of the most imbecelic (and wrong) replies ever
    >> seen in c.l.c,

    >
    > Maybe so, but many have considered some of *my* postings a
    > worthy contender for that hotly contested title :)
    >
    > Anyway, what's the accepted wisdom about doing the following?
    > (Is it at least well defined?)
    >
    > -------------
    > #include <stdio.h>
    >
    > int mygetchar (void)
    > {
    > printf ("My getchar\n");
    > return getchar();
    > }
    >
    > #define getchar() mygetchar()
    >
    > int main (void)
    > {
    > char c = getchar ();
    > printf ("got char '%c'\n", c);
    > return 0;
    > }
    >
    > -------------
    >


    (Should be "#define getchar mygetchar" without the parentheses).

    It's well-defined, because after your #define, all instances of getchar
    will be replaced with mygetchar. Since there ins't anything called
    'mygetchar' in stdio.h, there won't be any name conflicts at all. The
    preprocessor will wash away the text 'getchar', meaning no conflicts
    there, either.

    Whether it's a wise thing to do is questionable. It actually seems like
    a good idea in some debugging cases. (For example, redefining malloc to
    print a message to stdout about how much memory is allocated or when).
    I can't think of why it would be used in production code, though.

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    I know that area of town like the back of my head.
    Andrew Poelstra, Jun 23, 2006
    #10
  11. CBFalconer Guest

    goose wrote:
    >

    .... snip ...
    >
    > Anyway, what's the accepted wisdom about doing the following?
    > (Is it at least well defined?)
    >
    > -------------
    > #include <stdio.h>
    >
    > int mygetchar (void)
    > {
    > printf ("My getchar\n");
    > return getchar();
    > }
    >
    > #define getchar() mygetchar()
    >
    > int main (void)
    > {
    > char c = getchar ();
    > printf ("got char '%c'\n", c);
    > return 0;
    > }


    Strictly speaking it is UB. redefining entries in the standard
    library that have been referenced via the appropriate headers is
    not allowed.

    In practice, this particular one MAY work. No guarantees.

    I take it back. It will work, because you never use the define.
    Now think about why I said that.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
    CBFalconer, Jun 23, 2006
    #11
  12. goose Guest

    Andrew Poelstra wrote:
    > On 2006-06-23, goose <> wrote:


    <snipped>

    > >
    > > #define getchar() mygetchar()
    > >
    > > int main (void)
    > > {
    > > char c = getchar ();
    > > printf ("got char '%c'\n", c);
    > > return 0;
    > > }
    > >
    > > -------------
    > >

    >
    > (Should be "#define getchar mygetchar" without the parentheses).
    >


    Well, either should be okay (can't test it right now, no compiler
    handy) with the only difference being a matter of taste. I'd rather
    leave the () in 'cos then a simple scanning of the #define tells
    me that its used "just like a function call" further on.

    <snipped>

    > Whether it's a wise thing to do is questionable.


    We-ell ... the OP asked a very unwise thing to do anyway, one
    can only hope it is because he wants to put it to a /wise/
    use, and not willy-nilly trample all over the standard library.

    goose,
    goose, Jun 23, 2006
    #12
  13. goose Guest

    CBFalconer wrote:
    > goose wrote:
    > >

    > ... snip ...
    > >
    > > Anyway, what's the accepted wisdom about doing the following?
    > > (Is it at least well defined?)
    > >
    > > -------------
    > > #include <stdio.h>
    > >
    > > int mygetchar (void)
    > > {
    > > printf ("My getchar\n");
    > > return getchar();
    > > }
    > >
    > > #define getchar() mygetchar()
    > >
    > > int main (void)
    > > {
    > > char c = getchar ();
    > > printf ("got char '%c'\n", c);
    > > return 0;
    > > }

    >
    > Strictly speaking it is UB. redefining entries in the standard
    > library that have been referenced via the appropriate headers is
    > not allowed.
    >



    Hm? getchar (the #define) and getchar (the function prototype)
    don't necessarily step on each others toes, right? Or does
    "redefining" above mean "cannot reuse any symbols declared
    in the standard headers whether said symbols are prototype,
    #defines or typedefs?


    > In practice, this particular one MAY work. No guarantees.


    Never is with UB.

    >
    > I take it back. It will work, because you never use the define.
    > Now think about why I said that.
    >


    I did. I'm afraid I miss the point; AFAICT, getchar (the #define)
    should get text-substituted with mygetchar. I'd be happy to hear
    why that won't happen (Are we talking recursive mygetchar() bomb?
    thats simple enough to fix by replacing
    getchar ();
    in mygetchar with
    (getchar) ();
    although the @define follows the use of the function getchar()).

    goose
    goose, Jun 23, 2006
    #13
  14. CBFalconer Guest

    goose wrote:
    > CBFalconer wrote:
    >

    .... snip ...
    >>
    >> I take it back. It will work, because you never use the define.
    >> Now think about why I said that.

    >
    > I did. I'm afraid I miss the point; AFAICT, getchar (the #define)
    > should get text-substituted with mygetchar. I'd be happy to hear
    > why that won't happen (Are we talking recursive mygetchar() bomb?
    > thats simple enough to fix by replacing
    > getchar ();
    > in mygetchar with
    > (getchar) ();
    > although the @define follows the use of the function getchar()).


    You defined a functional macro with getchar(). You never used it,
    because the subsequent occurance of getchar was followed by a
    blank.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
    CBFalconer, Jun 23, 2006
    #14
  15. CBFalconer <> writes:
    > goose wrote:
    >> CBFalconer wrote:
    >>

    > ... snip ...
    >>>
    >>> I take it back. It will work, because you never use the define.
    >>> Now think about why I said that.

    >>
    >> I did. I'm afraid I miss the point; AFAICT, getchar (the #define)
    >> should get text-substituted with mygetchar. I'd be happy to hear
    >> why that won't happen (Are we talking recursive mygetchar() bomb?
    >> thats simple enough to fix by replacing
    >> getchar ();
    >> in mygetchar with
    >> (getchar) ();
    >> although the @define follows the use of the function getchar()).

    >
    > You defined a functional macro with getchar(). You never used it,
    > because the subsequent occurance of getchar was followed by a
    > blank.


    Nope.

    For a function-like macro, the left parenthesis must immediately
    follow the macro name (with no intervening whitespace) *in the macro
    definition*. In an invocation, the left parenthesis merely has to be
    the next preprocessing token.

    The whole point of a function-like macro is that it can be invoked as
    if it were an actual function. Disallowing whitespace in an
    invocation would have broken that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 23, 2006
    #15
  16. Andrew Poelstra <> writes:
    > On 2006-06-23, goose <> wrote:

    [snip]
    >> int mygetchar (void)
    >> {
    >> printf ("My getchar\n");
    >> return getchar();
    >> }
    >>
    >> #define getchar() mygetchar()

    [snip]
    >
    > (Should be "#define getchar mygetchar" without the parentheses).


    Not necessarily. With the parentheses it defines a function-like
    macro; you can then bypass the macro definition (and call the actual
    getchar() function) by enclosing the name in parentheses:

    (getchar)()

    With "#define getchar mygetchar", you can't do that. It depends on
    whether you want to allow the macro to be bypassed.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 23, 2006
    #16
  17. Morris Dovey Guest

    Richard Bos (in 4all.nl) said:

    | wrote:
    |
    || I want to redefine a function getchar() in header stdio.h ,but I
    || don't know what should I do.
    |
    | You should ask in a newsgroup dedicated to your particular compiler
    | suite, since in the general case the ISO C Standard declares this to
    | cause undefined behaviour.

    [ Original post never showed up at my server ]

    You can portably redefine standard library function invocations; but
    shouldn't tamper with the standard header files.

    At the link below is an example where calloc(), malloc(), realloc(),
    and free() have been redefined in a in order to produce a debug
    trace - but note that stdlib.h hasn't been touched.

    The file is the concatenation of two translation units, each of which
    is preceeded by a descriptive prolog.

    A lot of people have downloaded the code; and there haven't been any
    problems reported in the past six years, so I'll guess that there
    haven't been any portability issues.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/mrd/c/mtrace.c
    Morris Dovey, Jun 23, 2006
    #17
  18. On 2006-06-23, goose <> wrote:
    > Andrew Poelstra wrote:
    >> On 2006-06-23, goose <> wrote:

    >
    ><snipped>
    >
    >> >
    >> > #define getchar() mygetchar()
    >> >
    >> > int main (void)
    >> > {
    >> > char c = getchar ();
    >> > printf ("got char '%c'\n", c);
    >> > return 0;
    >> > }
    >> >
    >> > -------------
    >> >

    >>
    >> (Should be "#define getchar mygetchar" without the parentheses).
    >>

    >
    > Well, either should be okay (can't test it right now, no compiler
    > handy) with the only difference being a matter of taste. I'd rather
    > leave the () in 'cos then a simple scanning of the #define tells
    > me that its used "just like a function call" further on.
    >


    It would be easier to read, but if you accidentally put a space in
    there, getchar will be replaced with "() mygetchar()" instead of just
    "mygetchar()".

    ><snipped>
    >
    >> Whether it's a wise thing to do is questionable.

    >
    > We-ell ... the OP asked a very unwise thing to do anyway, one
    > can only hope it is because he wants to put it to a /wise/
    > use, and not willy-nilly trample all over the standard library.
    >


    Well, if he does abuse my advice, he won't be very employable, and
    it'll all eventually work itself out.

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    I know that area of town like the back of my head.
    Andrew Poelstra, Jun 23, 2006
    #18
  19. Andrew Poelstra <> writes:
    > On 2006-06-23, goose <> wrote:
    >> Andrew Poelstra wrote:
    >>> On 2006-06-23, goose <> wrote:

    >>
    >><snipped>
    >>
    >>> >
    >>> > #define getchar() mygetchar()
    >>> >
    >>> > int main (void)
    >>> > {
    >>> > char c = getchar ();
    >>> > printf ("got char '%c'\n", c);
    >>> > return 0;
    >>> > }
    >>> >
    >>> > -------------
    >>> >
    >>>
    >>> (Should be "#define getchar mygetchar" without the parentheses).
    >>>

    >>
    >> Well, either should be okay (can't test it right now, no compiler
    >> handy) with the only difference being a matter of taste. I'd rather
    >> leave the () in 'cos then a simple scanning of the #define tells
    >> me that its used "just like a function call" further on.
    >>

    >
    > It would be easier to read, but if you accidentally put a space in
    > there, getchar will be replaced with "() mygetchar()" instead of just
    > "mygetchar()".


    So don't put a space in there (in the macro definition, that is). In
    a function-like macro definition, the '(' must immediately follow the
    macro name, with no whitespace (one of the few cases where whitespace
    between tokens is significant). Anyone who writes macros had better
    know that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jun 24, 2006
    #19
  20. On Fri, 23 Jun 2006 15:36:50 UTC, Andrew Poelstra
    <> wrote:

    >
    > Whether it's a wise thing to do is questionable. It actually seems like
    > a good idea in some debugging cases. (For example, redefining malloc to
    > print a message to stdout about how much memory is allocated or when).
    > I can't think of why it would be used in production code, though.


    Come on and write a parser that needs to ungetc() more than the one
    single char that ungetc() allows.

    For performance you would simple getc() until you knows that the last
    2, 3, 4 chars were gotten too much - so ungetc() them simple to reset
    the input stream to a well known state, change the state of your state
    mashine to the right value and try again.

    myungetc() - ungets in an extended unget buffer
    hide the buffer from the rest of the application
    mygetc() - read from the extended unget buffer until it is emty,
    then from the stream further using the real getc()

    #define mygetc getc
    #define ungetc ungetc

    on the right place and you can adopt sorce code already written
    without the need to change each and any occurence of getc() and
    ungetc().

    Make things so simple as possible. Document your work right.

    I see no need for (f)gets() and other functions on a time where
    punchcards out of order. I can't even read text files when I don't
    know ith they are ordinated by DOS, WinDos, OS/2, Linux, AIX, SINIX,
    HPUX, /390, MAC or whatever ttransfered without any try to transform
    to your systems native text format. Transformchapters (readed as line
    of up 1 MB in size to lines of 80 char max, separate true chapters
    with really empty lines (\n) only, reformat tables (plain text format)
    to something readable,........

    getc() in cooperation with an extended ungetc() is the most possible
    solution in runtime and resource usage.

    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
    Herbert Rosenau, Jun 24, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robbie Hatley
    Replies:
    6
    Views:
    412
    Richard Herring
    Jun 28, 2006
  2. Replies:
    2
    Views:
    262
    Thomas J. Gritzan
    Feb 13, 2007
  3. Martin Boese

    Redefine a class with same name

    Martin Boese, Mar 9, 2008, in forum: Ruby
    Replies:
    2
    Views:
    98
    Martin Boese
    Mar 9, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    102
    PerlFAQ Server
    Feb 9, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    155
    PerlFAQ Server
    Apr 28, 2011
Loading...

Share This Page