473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Destructors in C

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
3 1866
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
8
by: johny smith | last post by:
If I have a simple class with say a couple of integers only is there any need for me to provide a destructor? thanks!
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.