Arrays inside of structures
Sometimes when doing interop, you want to have an array embedded inside of a struct. For example, something like:
struct data
{
int header;
int values[10];
}
that you either used in a call to interop, or with unsafe code to deal with an existing format - something like a network packet or a disk record.
You can do this in current versions of C#, but it's ugly. The most obvious way is:
struct Data
{
int header;
int value0;
int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
int value7;
int value8;
int value9;
}
But that's a bit ungainly, though, you *can* take the address of value0 and then use pointer arithmetic to access the various "array elements". There's a somewhat shorter version:
[StructLayout(LayoutKind.Sequential, Size=44)]
struct data
{
int header;
int values;
}
Setting the size to be 44. If you want to have more than one such array or have fields after the first one, you'll need to use LayoutKind.Explicit and put a FieldOffset attribute on every field.
In either case, you access the "array" by getting a pointer to the values field, and using pointer arithmetic.
To make this a little cleaner and less error-prone, in C# 2.0 we've added a fixed array syntax, allowing you to write:
struct data
{
int header;
fixed int values[10];
}
and getting the exact same behavior. You can then treat values as if it's a "c-style" array, using indexing, etc.
Because the underlying implementation is still using pointers and there's the possibility of going outside the allocated space, code generated using this feature requires using "unsafe", and therefore requires elevated privilege to execute.
Comments
- Anonymous
August 12, 2004
The comment has been removed - Anonymous
August 12, 2004
The comment has been removed - Anonymous
August 12, 2004
I just loaded up express 2005...
It doesn't seem to support multi dimensional arrays..
say I have this struct:
struct Data
{
int rows[2][64];
}
Will there be any way to apply fixed to that?
I've already used a work-around. I'm just curious. - Anonymous
August 12, 2004
I had the same problem when using custom types. In the end, I had to use a byte[] large enough to contain all the objects, then use Marshal.Copy. - Anonymous
August 12, 2004
Eric,
This example uses an array of ints. Will this new feature support complex value/reference types? - Anonymous
August 13, 2004
(I think) I posted this question before, but I guess it got lost in the comment moderation problem.
Anyway, I was wondering why the .NET Marshaler is unable to provide an automatic way to marshal structs with pointers to an array of structs.
For a project back in college, I wrote a C# wrapper for the winmm Mixer library, and I quickly found out that I had to manually marshal several of the structs. I know that I could use pointers and avoid the problem, but I wanted to avoid unsafe code (and I'm aware that any P/Invoke code isn't technically "safe").
Is there a good reason why isn't there an attribute to put on the array to allow this? Or has this been fixed in 1.1 or 2.0 and I'm just not aware of it? Is it perhaps impossible to cover every situation with marshaling attributes? - Anonymous
August 15, 2004
I want to sizeof the struct,how to? - Anonymous
August 22, 2004
Ping Back来自:blog.csdn.net - Anonymous
August 28, 2004
what about this:
[StructLayout(LayoutKind.Sequential, Pack=1/why not 4?-i thought that was the most common for unmanaged code/)]
struct ShiftData
{
[MarshalAs(UnmanagedType.ByValArray,ArraySubType=UnmanagedTyp.LPStruct, SizeConst=6)]
public SubShift[] Shifts;
} - Anonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/] - Anonymous
January 20, 2009
PingBack from http://www.hilpers-esp.com/440411-tipos-de-usuario