473,490 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What Can I Expect From This Union?

Within the following structure, TopStruct, I'd like to create 3 other
structures, 2 of which make up a union. The first structure will always
contain some data that I need and should never be overwritten, i.e. should
not be part of the union. The remaining 2, UnionData1 and UnionData2, make
up the union and are populated as needed.

I want to be certain that this code will work as planned. Specifically, if
the field offsets of the first union members are set to '0', will they
overwrite the data in the first structure, AlwaysContainsData? Or should
they be changed to the values indicated in the inline comments?

Essentially, I don't know if the FieldOffset attribute applies to the struct
in which it's called, e.g. UnionData1, or to the top-level struct,
TopStruct:
public struct TopStruct
{
[StructLayout( LayoutKind.Sequential )]
public struct AlwaysContainsData // Contains 262 bytes of data.
{
public short dataType;
public short rmsVolt;
public short rmsCurr;

[MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 )]
public short[] waveform;
}

// Begin the structs for the union.
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData1
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;
}

[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData2
{
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262 )"
?
public short[] waveform2;

[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
[ FieldOffset( 128 ) ] // or should it be "FieldOffset( 390 )"
?
public short[] waveform3;

}

} // End of TopStruct
Nov 15 '05 #1
5 1682

Hi Mike,

Because the FieldOffsetAttribute is applied to the UnionData class, I think
your FieldOffset
should be number relative to the UnionData class.
The member of the UnionData class will alloc memory after the first
structure AlwaysContainsData.

I think you can determine this by creating a unmanaged dlll consuming the
union.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Mike" <mi**@bogus.net>
| Subject: What Can I Expect From This Union?
| Date: Tue, 2 Sep 2003 15:15:17 -0400
| Lines: 57
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uj**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: dialup-171.75.39.7.dial1.washington1.level3.net
171.75.39.7
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:181638
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Within the following structure, TopStruct, I'd like to create 3 other
| structures, 2 of which make up a union. The first structure will always
| contain some data that I need and should never be overwritten, i.e. should
| not be part of the union. The remaining 2, UnionData1 and UnionData2,
make
| up the union and are populated as needed.
|
| I want to be certain that this code will work as planned. Specifically,
if
| the field offsets of the first union members are set to '0', will they
| overwrite the data in the first structure, AlwaysContainsData? Or should
| they be changed to the values indicated in the inline comments?
|
| Essentially, I don't know if the FieldOffset attribute applies to the
struct
| in which it's called, e.g. UnionData1, or to the top-level struct,
| TopStruct:
|
|
| public struct TopStruct
| {
| [StructLayout( LayoutKind.Sequential )]
| public struct AlwaysContainsData // Contains 262 bytes of data.
| {
| public short dataType;
| public short rmsVolt;
| public short rmsCurr;
|
| [MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 )]
| public short[] waveform;
| }
|
| // Begin the structs for the union.
| [ StructLayout( LayoutKind.Explicit ) ]
| public struct UnionData1
| {
| [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
| [ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262
)"
| ?
| public short[] waveform2;
| }
|
| [ StructLayout( LayoutKind.Explicit ) ]
| public struct UnionData2
| {
| [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
| [ FieldOffset( 0 ) ] // or should it be "FieldOffset( 262
)"
| ?
| public short[] waveform2;
|
| [ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128) ]
| [ FieldOffset( 128 ) ] // or should it be "FieldOffset( 390
)"
| ?
| public short[] waveform3;
|
| }
|
| } // End of TopStruct
|
|
|

Nov 15 '05 #2
Mike,
Essentially, I don't know if the FieldOffset attribute applies to the struct
in which it's called, e.g. UnionData1, or to the top-level struct,
TopStruct:


The offset is relative to the beginning of the "union" struct itself,
not any containing type.

Note that declaring a type nested inside another type does not
automatically give you a member of that nested type in the containing
type. Specifically, the TopStruct as it's written right now doesn't
have any members.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3
Thanks for your reply, Mattias.

If the offset is relative to the beginning of the union struct rather than
the containing struct, then the idea of UnionData1 and UnionData2 occupying
the same memory location is incorrect, isn't it?

To clarify matters, I wanted to have a data structure, TopStruct, that
contained some data that will be always populated (AlwaysContainsData
struct) by the data stream, and other data that would populate one of the
union structs or the other (UnionData1 or UnionData2), but not both. Hence,
I don't need to allocate memory for more than one union struct at a time,
though I need all the members within it.

Can I accomplish this?
-Mike
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
Mike,
Essentially, I don't know if the FieldOffset attribute applies to the structin which it's called, e.g. UnionData1, or to the top-level struct,
TopStruct:


The offset is relative to the beginning of the "union" struct itself,
not any containing type.

Note that declaring a type nested inside another type does not
automatically give you a member of that nested type in the containing
type. Specifically, the TopStruct as it's written right now doesn't
have any members.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #4
If the offset is relative to the beginning of the union struct rather than
the containing struct, then the idea of UnionData1 and UnionData2 occupying
the same memory location is incorrect, isn't it?
No, the idea is correct. But it's not always possible to realize. The
fact that you can't overlay object types with value types restricts
what you can do with explicit layout types.

Hence,
I don't need to allocate memory for more than one union struct at a time,


The effective size of a struct with explicit layout (union) is the
same regardless of which part of the union you use, so this will not
necessarily reduce memory allocation.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5
100,

You've given me food for thought. Thanks for your help.
-Mike

"100" <10*@100.com> wrote in message
news:O7**************@tk2msftngp13.phx.gbl...
Hi Mike
Is it posible to do something like the following. I haven't tried, though.

[ StructLayout( LayoutKind.Explicit ) ]
public struct TopStruct
{
[StructLayout( LayoutKind.Sequential )]
public struct AlwaysContainsData // Contains 262 bytes of data. {
....
}
[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData1
{
.....
}

[ StructLayout( LayoutKind.Explicit ) ]
public struct UnionData2
{
.....
}

[ FieldOffset( 0 ) ]
public AlwaysContainsData alwaysContainsData ;
[ FieldOffset( 262) ]
public UnionData1 unionData1
[ FieldOffset( 262) ]
public UnionData2 UnionData2
}

So if it works. changing
a field from unionData1 should change unionData2.

TopStruct topStruct = new TopStruct();
topStruct.unionData1.SomeField = ......

In your original example you made a mistake. Decalring a struct nested in
another struct/class doesn't make its members part of the outer struct/class members. It just changes the structure name scope.
So the data fields of AlwaysContainsData, UnionData1 and UnionData2 are not part of the TopStruct members and setting their members' FieldOffset
attributes cannot be releated to the TopStruct.
This is unlike the C++ anonimous structs and unions. To make them part of
the TopStruct you have to declare member variables of those struct types.

You might be able to do the following as well:

[ StructLayout( LayoutKind.Explicit ) ]
public struct TopStruct
{
[FieldOffset( 0 )]
int FirstAlwaysData;
[FieldOffset( 4 )]
int SecAlwaysData;
[FieldOffset( 8 )] //this and the next field occupy the same
memory
int FirstUnion1Data;
[FieldOffset( 8 )]
int FirstUnion2Data;
}

I haven't tested these ideas, though.

HTH
B\rgds
100

"Mike" <mi**@bogus.net> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
Thanks for your reply, Mattias.

If the offset is relative to the beginning of the union struct rather than the containing struct, then the idea of UnionData1 and UnionData2

occupying
the same memory location is incorrect, isn't it?

To clarify matters, I wanted to have a data structure, TopStruct, that
contained some data that will be always populated (AlwaysContainsData
struct) by the data stream, and other data that would populate one of the union structs or the other (UnionData1 or UnionData2), but not both.

Hence,
I don't need to allocate memory for more than one union struct at a time, though I need all the members within it.

Can I accomplish this?
-Mike
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
Mike,

>Essentially, I don't know if the FieldOffset attribute applies to the

struct
>in which it's called, e.g. UnionData1, or to the top-level struct,
>TopStruct:

The offset is relative to the beginning of the "union" struct itself,
not any containing type.

Note that declaring a type nested inside another type does not
automatically give you a member of that nested type in the containing
type. Specifically, the TopStruct as it's written right now doesn't
have any members.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Nov 15 '05 #6

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

Similar topics

4
7594
by: jonnychang | last post by:
What is the purpose of using union in this class? Class A { public: union { int age; char * name; double amount; } };
4
2010
by: Matej Pivoluska | last post by:
Hello, could anybody explain me, what abbrevation POD means? I think, this is just human-language (English vs. Slovak) problem. Thanks, -- mP
3
3540
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
2
2501
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
10
5046
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
4
2924
by: Girish | last post by:
I have 2 differesnt defination of same Union as below and a piece of code for printing size of Union and its members.. union U { union U { int i; int j; }a;
18
1911
by: rajpal_jatin | last post by:
int main() { int k; union jatin{ int i :5; char j :2; }; union jatin rajpal; k= sizeof(rajpal);
3
7698
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
3
1607
by: mzahid | last post by:
I have 3 tables student(s_id,s_name),course(c_id,c_name) and student_course(s_id(FK),c_id(FK)). I would Like to select those students Name who have enrolled more then and equal to 3 courses. What...
0
7108
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
6967
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
7181
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...
1
6847
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
7352
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...
0
4565
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.