Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   atreturn() like atexit() (http://www.velocityreviews.com/forums/t440757-atreturn-like-atexit.html)

Alex Vinokur 01-04-2006 04:48 PM

atreturn() like atexit()
 
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


Kenneth Brody 01-04-2006 05:29 PM

Re: atreturn() like atexit()
 
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:ThisIsASpamTrap@gmail.com>



Eric Sosman 01-04-2006 05:43 PM

Re: atreturn() like atexit()
 
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
esosman@acm-dot-org.invalid

Chuck F. 01-04-2006 05:50 PM

Re: atreturn() like atexit()
 
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/>

Pedro Graca 01-04-2006 09:18 PM

Re: atreturn() like atexit()
 
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!


All times are GMT. The time now is 10:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.