473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structs and destructors

since structs are value types, is a destructor of a struct immediately
called when the struct goes out of scope? this is definitely not the case
with class-objects since they are allocated dynamically are cleaned up by
the gc at a undefined time.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #1
4 2978
A value type doesn't need a destructor (finalizer) because it's reclaimed by
the normal movement of the top of the stack. Value types that are boxed are
managed by GC but only because the GC simply reclaims the heap that they
occupy.

If you search for "struct AND finalizer" in the MSDN help you'll see about 4
articles that although they don't mention struct and finalizer in the same
sentence, have a wealth of information. In particular "Performanc e
Considerations for Run-Time Technologies in the .NET Framework" is an
excellent read.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Blog http://bobpowelldotnet.blogspot.com

"codymanix" <do************ *********@gmx.d e> wrote in message
news:u3******** ******@TK2MSFTN GP11.phx.gbl...
since structs are value types, is a destructor of a struct immediately
called when the struct goes out of scope? this is definitely not the case
with class-objects since they are allocated dynamically are cleaned up by
the gc at a undefined time.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 15 '05 #2
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message news:<uZ******* ******@TK2MSFTN GP11.phx.gbl>.. .
A value type doesn't need a destructor (finalizer) because it's reclaimed by
the normal movement of the top of the stack.


It's precisely because value types are "declaimed by..." that makes
destructors potentially useful. On the other hand, destructors for
reference types are of dubious value (no pun intended), because GC is
not deterministic.

Sam
Nov 15 '05 #3
Hi Sam,
It's precisely because value types are "declaimed by..." that makes
destructors potentially useful. On the other hand, destructors for
reference types are of dubious value (no pun intended), because GC is
not deterministic.


IMHO the destructor are of the more importance when you are dealing with
unmanaged resources, it does not matter if the type is reference or value,
when you have to manually release any resource then you need a destructor,
otherwise the GC ( in reference types) or the stack movement ( value types )
will handle pretty well the memory, there is no need of a destructor in
these case unless you need a performance gain.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 15 '05 #4
> A value type doesn't need a destructor (finalizer) because it's reclaimed
by
the normal movement of the top of the stack. Value types that are boxed are managed by GC but only because the GC simply reclaims the heap that they
occupy.


struct FileWrapperRAII
{
File f;
public FileWrapperRAII (string path) { f = new File(path); }
~FileWrapperRAI I() { f.Close(); }
}

when the struct is popped off the stack the file can be immediately be
closed instead
of using the "using-clause" or waiting for the GC.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21377
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...
26
448
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would...
26
2698
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...
8
1799
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...
9
5791
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct ValueStruct { double x ; double y ; ValueStruct() { x = y = double(0) ; } // error * } ;
3
1157
by: Greg | last post by:
In the following example, where is "n" allocated (e.g. managed heap?) ? public ref class N { .... .... } main() {
19
2535
by: desktop | last post by:
There is a lot of info on this topic on google. But in Bjarne Stroustrup 's book page 225 he writes: "The declaration of Date in the previous subsection (declared as a struct) provides a set of functions for manipulating a Date. However, it does not specify that those functions should be the only ones to depend directly on Date ’s...
43
3778
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a...
6
5146
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
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
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...
1
7665
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...
0
6277
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...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
0
3651
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...
1
2105
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

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.