By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

Destructors in C

P: n/a
If I have code like this

int f() {
// ... stuff ...
g();
if(x > 0) return (x+4);
// ... more stuff ...
always_call(z);
return y;
}

can I insure that always_call will be called even when the conditional
(x > 0) succeeds and when g() might contain non-local jumps?

I'm basically curious what C++-hating hard-core C programmers use to
achieve what C++ destructors give you. The common opinion is that their
choices are not good.

Jan 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ico
al*******@gmail.com wrote:

int f() {
// ... stuff ...
g();
if(x > 0) return (x+4);
// ... more stuff ...
always_call(z);
return y;
}

can I insure that always_call will be called even when the conditional
(x > 0) succeeds and when g() might contain non-local jumps?
I do not really understand what you want to achieve here; You choose to
return from the function prematurely, but still you want the code
*after* the return to execute. To my knowledge, this is not what
destructors are about.

Having more then one return in a function is considered bad practice by
some, because it obfuscates program flow.

I'm basically curious what C++-hating hard-core C programmers use to
achieve what C++ destructors give you. The common opinion is that their
choices are not good.


I do a lot of object-oriented programming with C. My 'object' usually is
a pointer to a struct of a specific type. I create a function new()
which allocates, initializes and returns a pointer to this type. I
create function del() which 'destructs' and frees the object. As far as
I know, this is similar to C++ constructors and destructors.

I do not think you gave good example for talking about destructors.
Destructors are not 'pieces of code that run after you have returned
from a function'.

Ico
--
:wq
^X^Cy^K^X^C^C^C^C
Jan 10 '06 #2

P: n/a
This is not true

After a return statement no code is executed it the function

So don't post these silly things.
Greetings Olaf

Jan 10 '06 #3

P: n/a
ol************@gmail.com wrote:
This is not true
/What/ is not true?
After a return statement no code is executed it the function

So don't post these silly things.


You might try and understand what the OP is asking before you
brush them off like that.

The OP asked:
If I have code like this int f() {
// ... stuff ...
g();
if(x > 0) return (x+4);
// ... more stuff ...
always_call(z);
return y;
} can I insure that always_call will be called even when the conditional
(x > 0) succeeds and when g() might contain non-local jumps?


The answer is that, no, in C you can't do that, except by doing it
all "by hand"; you must be able to trap any non-local jumps out of
g (which you have to do with the connivance of g and everything it
calls that might longjump) and you have to arrange that always-call
is executed before every return. In C++ the compiler handles this
for destructor code; in C you have to /be/ that compiler (without
the advantage of having total control over the generated code).

--
Chris "believes seventeen improbable things before coffee" Dollin
Seventeen, forty-two - what else is there?
Jan 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.