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

Layout.Explicit with managed code for UNION structs

I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++.
This is not for passing data to unmanaged code, it is to save memory space because field X will either be short integer or double, and other info in the struct can be used to determine if X should be interpreted as short integer or double.

I have found the docs for [StructLayout(LayoutKind.Explicit)] and for [FieldOffset(0)].

However, one doc state that these fields only control marshalling to unmanged code and does not impact managed storage, while another doc simply states that these attributes may be used to control field layouts.

Does anyone know how this is really implemented? I would like to use it to reduce my memory requirements with large arrays of this struct.


--------------------------------
From: Fuzzy

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>sVbH/6qB6kmbeSIYWHUaOw==</Id>
Nov 17 '05 #1
1 2399
IIRC, you *can* directly control the storagelayout with these attributes,
but .NET does't allow overlapping of fields to maintain type safety, that
is, you cannot use this to implement a UNION.
If you want to interpret a certain field as another datatyp use properties
which make use of the Marshal class or Bitconverter to reinterpret existing
data fields.

"Fuzzy via .NET 247" <an*******@dotnet247.com> schrieb im Newsbeitrag
news:u$**************@TK2MSFTNGP09.phx.gbl...
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field X will either be short integer or double, and other info in
the struct can be used to determine if X should be interpreted as short
integer or double.
I have found the docs for [StructLayout(LayoutKind.Explicit)] and for [FieldOffset(0)].
However, one doc state that these fields only control marshalling to unmanged code and does not impact managed storage, while another doc simply
states that these attributes may be used to control field layouts.
Does anyone know how this is really implemented? I would like to use it to reduce my memory requirements with large arrays of this struct.

--------------------------------
From: Fuzzy

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>sVbH/6qB6kmbeSIYWHUaOw==</Id>

Nov 17 '05 #2

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

Similar topics

73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
4
by: Chua Wen Ching | last post by:
Hi there, I had a union in C: typedef union TagMessage { struct { unsigned char avalue; unsigned char bvalue; } a;
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
3
by: cody | last post by:
I got a warning from fxcop from the declararation of a struct. the warning: "Structures with explicit layout containing misaligned fields cause crashes on 64 bit platforms" the struct:
2
by: Jack Ukleja | last post by:
Hi guys, I'm trying to figure out a way to do reflection on structs which are in a legacy c++ header file. Basically I want to write a C++/CLI program that lets me edit the binary data in the...
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
9
by: dspfun | last post by:
Hi! I have stumbled on the following C question and I am wondering wether the answer is implementation specific or if the answer is always valid, especially question b). BRs! ...
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
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,...

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.