473,398 Members | 2,403 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,398 software developers and data experts.

C++/CLI Unions and Garbage collection


1.) In C++/CLI, I have the following structures defined, which work as
unions:
[StructLayout(LayoutKind::Explicit)]
public ref struct One
{
[FieldOffset(0)] Int32 a;
};

[StructLayout(LayoutKind::Explicit)]
public ref struct Two
{
[FieldOffset(0)] Int32 a;
};

[StructLayout(LayoutKind::Explicit)]
public ref struct MyUnion
{
[FieldOffset(0)] One^ one;
[FieldOffset(0)] Two^ two;
};
Then I use these structures from C# and write the following:

MyUnion mu = new MyUnion();
mu.one = new One();
mu.two = new Two(); // is the instance of class One now available for
GC?
mu.two.a = 42;

Has the memory allocated for One now been made available for garbage
collection?
Obviousy it would in the typical case when One^ and Two^ don't overlap. But
what if they do? Does the garbage collector still track this?

------------------------------------

2.) For my second question: is it known what the size of a reference
handle is?
For example, if I were to instead write MyUnion as:

[StructLayout(LayoutKind::Explicit)]
public ref struct MyUnion
{
[FieldOffset(0)] One^ one;
[FieldOffset(4)] Two^ two; // <--- note different field offset
};

Am I allowed to depend on One^ taking up four bytes?
Thanks!






Jan 20 '06 #1
2 2540
minorguy wrote:
Then I use these structures from C# and write the following:

MyUnion mu = new MyUnion();
mu.one = new One();
mu.two = new Two(); // is the instance of class One now available
for GC?
mu.two.a = 42;

Has the memory allocated for One now been made available for garbage
collection?
Yes.
Obviousy it would in the typical case when One^ and Two^ don't
overlap. But what if they do? Does the garbage collector still track
this?
------------------------------------

2.) For my second question: is it known what the size of a reference
handle is?
For example, if I were to instead write MyUnion as:

[StructLayout(LayoutKind::Explicit)]
public ref struct MyUnion
{
[FieldOffset(0)] One^ one;
[FieldOffset(4)] Two^ two; // <--- note different field
offset };

Am I allowed to depend on One^ taking up four bytes?


No. The size of a reference is (I believe) undefined. It's certainly not 4
bytes on a 64-bit CLR.

-cd
Jan 20 '06 #2

Thanks for your response.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:Oi**************@TK2MSFTNGP14.phx.gbl...

No. The size of a reference is (I believe) undefined. It's certainly not
4 bytes on a 64-bit CLR.


Yeah, I figured it would be different on 64-bit. What I was looking for was
whether it was defined at all, like C++ pointers are. Thanks.

Jan 20 '06 #3

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

Similar topics

6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.