M
minorguy
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!
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!