atreturn() like atexit()

Discussion in 'C Programming' started by Alex Vinokur, Jan 4, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    atexit() sets a function to be called when the program exits.

    Is it possible to create similar mechanism for functions too?
    For instance,
    to create function atreturn(void (*)(void)) that
    sets a function to be called
    when a function of 'void (*)(void) type' returns.


    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jan 4, 2006
    #1
    1. Advertising

  2. Alex Vinokur wrote:
    >
    > atexit() sets a function to be called when the program exits.
    >
    > Is it possible to create similar mechanism for functions too?
    > For instance,
    > to create function atreturn(void (*)(void)) that
    > sets a function to be called
    > when a function of 'void (*)(void) type' returns.


    If you were writing the C compiler, you could certainly implement such a
    thing. However, to implement it otherwise, you would need to make an
    explicit call before returning.

    You could make some struct that holds the info you need (like a linked
    list of function pointers), and call an init function.

    For example:

    int myfunc()
    {
    AtReturn *atret = InitAtReturn();

    Then call "atreturn" as appropriate:

    AddAtReturn(atret, myotherfunc);

    And, wherever you would have "return value;" you replace it with a
    macro reference instead:

    DoAtReturn(atret,value);

    where "DoAtReturn" is a macro such as:

    #define DoAtReturn(ret_ptr,value) RealAtReturn(ret_ptr); return value

    Note that InitAtReturn would malloc the struct, and RealAtReturn would
    free it.

    I'm sure that there are some problems with this, as written (which I'm
    sure others will quickly point out), but it should be a useful starting
    point for a "real" implementation.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Jan 4, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Eric Sosman Guest

    Alex Vinokur wrote:
    > atexit() sets a function to be called when the program exits.
    >
    > Is it possible to create similar mechanism for functions too?
    > For instance,
    > to create function atreturn(void (*)(void)) that
    > sets a function to be called
    > when a function of 'void (*)(void) type' returns.


    The language doesn't provide anything along these lines.
    Actually, "these lines" might be any of four (or maybe more)
    different things:

    1. A specific caller wants something special to happen
    when the function it calls returns. Easy: Just Do It.

    2. A specific callee wants something special to happen
    when it returns. Easy: Just Do It. In the case of
    a function with many `return' statements, it may be
    convenient to wrap it with another.

    3. Some piece of the program wants something special to
    happen whenever a specific function returns, no matter
    who calls it. You can probably reduce this to an
    instance of Case 2; in this situation a wrapper is
    almost certainly the way to proceed.

    4. Some piece of the program wants something special to
    happen whenever any function with a specified signature
    returns. I can't think of any good way to do this (nor
    of any good reason to want to do it).

    Perhaps what you really want is a debugger.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 4, 2006
    #3
  4. Alex Vinokur

    Chuck F. Guest

    Alex Vinokur wrote:
    > atexit() sets a function to be called when the program exits.
    >
    > Is it possible to create similar mechanism for functions too?


    typedef .... FUNC .... /* as needed */

    ..... afunc(...) {} /* suitable for FUNC */

    void sillyfunc(void) {
    FUNC onexit;

    onexit = NULL;
    ....
    if (cond) onexit = afunc;
    ....
    if (afunc) afunc();
    return;
    }

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Jan 4, 2006
    #4
  5. Alex Vinokur

    Pedro Graca Guest

    Chuck F. wrote:
    >
    > onexit
    >


    <delurk mode="ot">
    My first reaction was to read that as two equal-sized words :)

    I sometimes use the second three-letter word for temporary
    variables (I like it better than `tmp`).
    </delurk>

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!
     
    Pedro Graca, Jan 4, 2006
    #5
    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. Serve Laurijssen

    atexit and global destructors

    Serve Laurijssen, Jan 15, 2004, in forum: C++
    Replies:
    3
    Views:
    7,005
    tom_usenet
    Jan 15, 2004
  2. JKop
    Replies:
    8
    Views:
    464
  3. Bengt Richter
    Replies:
    0
    Views:
    294
    Bengt Richter
    Aug 25, 2004
  4. Chris Gorton

    atexit not being executed

    Chris Gorton, May 3, 2005, in forum: Python
    Replies:
    0
    Views:
    399
    Chris Gorton
    May 3, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page