473,778 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1894
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
21393
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 an idea that we can have private constructors and destructors but am not able to find a situation where they can be used... Regards RVG rajeshgarg@opussoft.com
3
2717
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 Nuno Barros
12
1817
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 standard says that empty() has constant complexity. If it actually called the destructor for each object, it seems to me it would have linear complexity. Does empty() call the destructor for each object in the container? If yes, why is it described...
8
1430
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
1890
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 destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be virtual always. What exactly is the reason for not having it so?
26
2724
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 (and rarely). This always happens during process shutdown, after some thread has called System.Environment.Exit(). Clearly, some sort of race condition. Note that what follows only applies to destructors that are called when the process shuts...
8
1807
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 class is created, I dynamically allocate an object of the class with the legacy C++ code. However because the __value class has no destructor, I can never release that allocated memory. Why does a __value class allow no destructor ? Without it I...
6
7531
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
5166
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 { blah(); virtual ~blah();
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8957
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2865
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.