473,587 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(La youtKind.Explic it)] 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/6qB6kmbeSIYWHUa Ow==</Id>
Nov 17 '05 #1
1 2412
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*******@dotn et247.com> schrieb im Newsbeitrag
news:u$******** ******@TK2MSFTN GP09.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(La youtKind.Explic it)] 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/6qB6kmbeSIYWHUa Ow==</Id>

Nov 17 '05 #2

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

Similar topics

73
3987
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
4
13899
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
1628
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 particular I want to implement the op_Assign (=) operator for a __value struct.
3
2036
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
2505
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 structs, but there are many of these structs so it has to be automated. Also I do not want to have to modify the legacy struct definitions. Firstly if I simply include the .h file then none of the structs appear in the assembly. So I guess first...
12
6692
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: public enum Days { Sunday };
28
3623
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 anyway, so what's the point? Without this restriction the compiler could layout the structure in the most efficient way possible, for some definition of efficient. It would be easy enough to turn this reordering off with a compiler specific...
9
5698
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! ---------------------------------------------------- Following C code runs on a "little endian" machine that has a 32-bit
7
6409
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
7924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.