473,399 Members | 3,038 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,399 software developers and data experts.

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 2973
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 "Performance
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.de> wrote in message
news:u3**************@TK2MSFTNGP11.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@_spamkiller_bobpowell.net> wrote in message news:<uZ*************@TK2MSFTNGP11.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); }
~FileWrapperRAII() { 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
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...
26
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...
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...
9
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...
3
by: Greg | last post by:
In the following example, where is "n" allocated (e.g. managed heap?) ? public ref class N { .... .... } main() {
19
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...
43
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...
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 {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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,...
0
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...
0
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,...
0
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...

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.