473,382 Members | 1,362 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,382 software developers and data experts.

unions in C#

I have been porting some C++ code to .net. I know C# doesn't have unions but
in order to write a particular algorythm efficiently i have a need to
address memory in in 2 different ways. As individual elements and also as an
array. I have tried the following (along with several variations) and none
work. Anyone have an alternative to what I am trying to do? The problem
comes when I want to have the int array occupy the same space as the 6 ints
defined above it. I would even try an 'unsafe' block using the 'fixed'
method but am having trouble getting that to work also. Seems there might be
some conflict with the other attributes I require possibly.
[StructLayout(LayoutKind.Explicit, Size = 24)]
public class TRIANGLE

{

[FieldOffset(0)] public int a;

[FieldOffset(4)] public int b;

[FieldOffset(8)] public int c;

[FieldOffset(12)] public int a_to_b;

[FieldOffset(16)] public int b_to_c;

[FieldOffset(20)] public int c_to_a;

[FieldOffset(0)] public int[] tri_pnt;

}

Any help would be greatly appreciated.

thanks

brian
Oct 25 '06 #1
4 1698
You could forget that kind of implementation.
See .NET help for more information:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxinterop/html/96a62265-dcf9-4608-bc0a-1f762ab9f48e.htm

Meanwhile you could try this C# developer friendly code:
public class Triangle : ICollection
{
public int A;
public int B;
public int C;
public int this[int index]
{
get
{
switch(index)
{
case 0:
return A;
case 1:
return B;
case 2:
return C;
default:
throw new ArgumentOutOfRangeException("idnex");
}
}
set
{
switch(index)
{
case 0:
A = value;
case 1:
B = value;
case 2:
C = value;
default:
throw new ArgumentOutOfRangeException("idnex");
}
}
}
public int Count { get { return 3; } }
public IEnumerator GetEnumerator()
{
yield return A;
yield return B;
yield return C;
}
}

"Brian" <mccorb(remove_this)@cox.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I have been porting some C++ code to .net. I know C# doesn't have unions
but in order to write a particular algorythm efficiently i have a need to
address memory in in 2 different ways. As individual elements and also as
an array. I have tried the following (along with several variations) and
none work. Anyone have an alternative to what I am trying to do? The
problem comes when I want to have the int array occupy the same space as
the 6 ints defined above it. I would even try an 'unsafe' block using the
'fixed' method but am having trouble getting that to work also. Seems there
might be some conflict with the other attributes I require possibly.
[StructLayout(LayoutKind.Explicit, Size = 24)]
public class TRIANGLE

{

[FieldOffset(0)] public int a;

[FieldOffset(4)] public int b;

[FieldOffset(8)] public int c;

[FieldOffset(12)] public int a_to_b;

[FieldOffset(16)] public int b_to_c;

[FieldOffset(20)] public int c_to_a;

[FieldOffset(0)] public int[] tri_pnt;

}

Any help would be greatly appreciated.

thanks

brian


Oct 25 '06 #2

"Brian" <mccorb(remove_this)@cox.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
|I have been porting some C++ code to .net. I know C# doesn't have unions
but
| in order to write a particular algorythm efficiently i have a need to
| address memory in in 2 different ways. As individual elements and also as
an
| array. I have tried the following (along with several variations) and none
| work. Anyone have an alternative to what I am trying to do? The problem
| comes when I want to have the int array occupy the same space as the 6
ints
| defined above it. I would even try an 'unsafe' block using the 'fixed'
| method but am having trouble getting that to work also. Seems there might
be
| some conflict with the other attributes I require possibly.
|
|
| [StructLayout(LayoutKind.Explicit, Size = 24)]
| public class TRIANGLE
|
| {
|
| [FieldOffset(0)] public int a;
|
| [FieldOffset(4)] public int b;
|
| [FieldOffset(8)] public int c;
|
| [FieldOffset(12)] public int a_to_b;
|
| [FieldOffset(16)] public int b_to_c;
|
| [FieldOffset(20)] public int c_to_a;
|
| [FieldOffset(0)] public int[] tri_pnt;
|
| }
|
|
|
| Any help would be greatly appreciated.
|
|
|
| thanks
|
| brian
|
|

If you don't mind to introduce some unsafe context in your applications,
make your TRIANGLE a struct and declare your array of int's as fixed,
something like this will do:

{[StructLayout(LayoutKind.Explicit)]
public unsafe struct TRIANGLE
{
[FieldOffset(0)] public int a;
[FieldOffset(4)] public int b;
[FieldOffset(8)] public int c;
[FieldOffset(12)] public int a_to_b;
[FieldOffset(16)] public int b_to_c;
[FieldOffset(20)] public int c_to_a;
[FieldOffset(0)] public fixed int tri_pnt[6];
}

Note that you can't access fixed elements from safe context, nor can you
"foreach" variable of that type.

....
TRIANGLE t = new TRIANGLE();
t.a = 1; t.b = 2; t.c = 3; t.a_to_b = 4;
t.b_to_c = 5; t.c_to_a = 6;
int tSize = Marshal.SizeOf(t)/sizeof(int);
unsafe {
for(int i = 0; i < tSize; i++)
Console.WriteLine(t.tri_pnt[i]);
}

Willy.

Oct 25 '06 #3
Cool!!

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:e6****************@TK2MSFTNGP03.phx.gbl...
>
"Brian" <mccorb(remove_this)@cox.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
|I have been porting some C++ code to .net. I know C# doesn't have unions
but
| in order to write a particular algorythm efficiently i have a need to
| address memory in in 2 different ways. As individual elements and also
as
an
| array. I have tried the following (along with several variations) and
none
| work. Anyone have an alternative to what I am trying to do? The problem
| comes when I want to have the int array occupy the same space as the 6
ints
| defined above it. I would even try an 'unsafe' block using the 'fixed'
| method but am having trouble getting that to work also. Seems there
might
be
| some conflict with the other attributes I require possibly.
|
|
| [StructLayout(LayoutKind.Explicit, Size = 24)]
| public class TRIANGLE
|
| {
|
| [FieldOffset(0)] public int a;
|
| [FieldOffset(4)] public int b;
|
| [FieldOffset(8)] public int c;
|
| [FieldOffset(12)] public int a_to_b;
|
| [FieldOffset(16)] public int b_to_c;
|
| [FieldOffset(20)] public int c_to_a;
|
| [FieldOffset(0)] public int[] tri_pnt;
|
| }
|
|
|
| Any help would be greatly appreciated.
|
|
|
| thanks
|
| brian
|
|

If you don't mind to introduce some unsafe context in your applications,
make your TRIANGLE a struct and declare your array of int's as fixed,
something like this will do:

{[StructLayout(LayoutKind.Explicit)]
public unsafe struct TRIANGLE
{
[FieldOffset(0)] public int a;
[FieldOffset(4)] public int b;
[FieldOffset(8)] public int c;
[FieldOffset(12)] public int a_to_b;
[FieldOffset(16)] public int b_to_c;
[FieldOffset(20)] public int c_to_a;
[FieldOffset(0)] public fixed int tri_pnt[6];
}

Note that you can't access fixed elements from safe context, nor can you
"foreach" variable of that type.

...
TRIANGLE t = new TRIANGLE();
t.a = 1; t.b = 2; t.c = 3; t.a_to_b = 4;
t.b_to_c = 5; t.c_to_a = 6;
int tSize = Marshal.SizeOf(t)/sizeof(int);
unsafe {
for(int i = 0; i < tSize; i++)
Console.WriteLine(t.tri_pnt[i]);
}

Willy.

Oct 26 '06 #4
Brian <mccorb(remove_this)@cox.netwrote:
I have been porting some C++ code to .net. I know C# doesn't have unions but
in order to write a particular algorythm efficiently i have a need to
address memory in in 2 different ways. As individual elements and also as an
array. I have tried the following (along with several variations) and none
work. Anyone have an alternative to what I am trying to do? The problem
comes when I want to have the int array occupy the same space as the 6 ints
defined above it. I would even try an 'unsafe' block using the 'fixed'
method but am having trouble getting that to work also. Seems there might be
some conflict with the other attributes I require possibly.
[StructLayout(LayoutKind.Explicit, Size = 24)]
public class TRIANGLE

{

[FieldOffset(0)] public int a;

[FieldOffset(4)] public int b;

[FieldOffset(8)] public int c;

[FieldOffset(12)] public int a_to_b;

[FieldOffset(16)] public int b_to_c;

[FieldOffset(20)] public int c_to_a;

[FieldOffset(0)] public int[] tri_pnt;

}

Any help would be greatly appreciated.

Brian ... take a look at the System.BitConverter class. You might be able to
get it to work in such a way to be useful to you. I do believe that it copies
the results, so you will constantly be thunking the data back and forth, but
it should be more efficient than some other methods. Nowhere near the
efficiency of a C++ union though.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Oct 26 '06 #5

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

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
8
by: SteveM | last post by:
The general consensus I am getting is that nobody really uses unions much (engineers here at work) but this is an academic exercise for me so I am looking for an answer (I know there may be better...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.