Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > atreturn() like atexit()

Reply
Thread Tools

atreturn() like atexit()

 
 
Alex Vinokur
Guest
Posts: n/a
 
      01-04-2006
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

 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      01-04-2006
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: <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-04-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-04-2006
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/>
 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      01-04-2006
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!
 
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
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Gmail-like or Orkut-like interface Blmn ASP .Net 0 12-03-2004 01:27 AM
any EL-like MessageFormat-like classes? Ittay Dror Java 1 07-29-2004 05:38 PM
My wife's like kinda what my wife's like { Listerine Enema } Computer Support 0 06-01-2004 01:38 AM
Would like to make a cell look like a dropdown box in the edit mode Alex ASP .Net 0 04-25-2004 09:10 PM



Advertisments