atreturn() like atexit()

K

Kenneth Brody

Alex said:
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:[email protected]>
 
E

Eric Sosman

Alex said:
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.
 
C

Chuck F.

Alex said:
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/>
 
P

Pedro Graca

Chuck said:

<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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top