Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# array within a struct

Tags:

c#

Want to do this: (EDIT: bad sample code, ignore and skip below)

struct RECORD {     char[] name = new char[16];     int dt1; } struct BLOCK {     char[] version = new char[4];     int  field1;     int  field2;     RECORD[] records = new RECORD[15];     char[] filler1 = new char[24]; } 

But being unable to declare array sizes in struct, how do I reconfigure this?

EDIT: The reason for the layout is I'm using BinaryReader to read a file written with C structs. Using BinaryReader, and a C# struct union (FieldOffset(0)), I'm wanting to load the header as a byte array, then read it as it was intended originally.

[StructLayout(LayoutKind.Sequential)] unsafe struct headerLayout {     [FieldOffset(0)]     char[] version = new char[4];     int fileOsn;     int fileDsn;     // and other fields, some with arrays of simple types }  [StructLayout(LayoutKind.Explicit)] struct headerUnion                  // 2048 bytes in header {     [FieldOffset(0)]     public byte[] headerBytes;      // for BinaryReader     [FieldOffset(0)]     public headerLayout header;     // for field recognition } 
like image 577
Robert Kerr Avatar asked Jan 02 '12 18:01

Robert Kerr


People also ask

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C or C++ same?

While C and C++ may sound similar, their features and usage differ. C is a procedural programming language that support objects and classes. On the other hand C++ is an enhanced version of C programming with object-oriented programming support.


2 Answers

Use fixed size buffers:

[StructLayout(LayoutKind.Explicit)] unsafe struct headerUnion                  // 2048 bytes in header {     [FieldOffset(0)]     public fixed byte headerBytes[2048];           [FieldOffset(0)]     public headerLayout header;  } 

Alternativ you can just use the struct and read it with the following extension method:

private static T ReadStruct<T>(this BinaryReader reader)         where T : struct {     Byte[] buffer = new Byte[Marshal.SizeOf(typeof(T))];     reader.Read(buffer, 0, buffer.Length);     GCHandle handle = default(GCHandle);     try     {         handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);         return (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));     }     finally     {         if (handle.IsAllocated)              handle.Free();     } } 
like image 135
Felix K. Avatar answered Oct 01 '22 16:10

Felix K.


Unmanaged structures can contain embedded arrays. By default, these embedded array fields are marshaled as a SAFEARRAY. In the following example, s1 is an embedded array that is allocated directly within the structure itself.

Unmanaged representation struct MyStruct {     short s1[128]; } 

Arrays can be marshaled as UnmanagedType.ByValArray, which requires you to set the MarshalAsAttribute.SizeConst field. The size can be set only as a constant. The following code shows the corresponding managed definition of MyStruct. C#VB

[StructLayout(LayoutKind.Sequential)] public struct MyStruct {    [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)] public short[] s1; } 
like image 29
David Yachnis Avatar answered Oct 01 '22 16:10

David Yachnis