1

Topic: [unsafe] what that nonsense

I welcome all! I will begin with the simple code [StructLayout (LayoutKind. Sequential, Pack = 4)] public struct Example {public bool a1; public bool a2; public bool a3; public bool a4; public bool a5; public bool a6; public bool a7; public bool a8; public bool a9;} static unsafe void Main (string [] args) {Example ex = new Example (); ex.a1 = true; ex.a2 = true; ex.a3 = true; ex.a4 = true; ex.a5 = true; ex.a6 = true; ex.a7 = true; int size = Marshal. SizeOf <Example> (); IntPtr ptr = Marshal. AllocHGlobal (size); Marshal. StructureToPtr (ex, ptr, false); Example* exPtr = (Example *) ptr. ToPointer (); Example exNew = Marshal. PtrToStructure <Example> (ptr); if (ex.a1 == exNew.a1 && ex.a2 == exNew.a2 && ex.a3 == exNew.a3 && ex.a4 == exNew.a4 && ex.a5 == exNew.a5 && ex.a6 == exNew.a6 && ex.a7 == exNew.a7 && ex.a8 == exNew.a8 && ex.a9 == exNew.a9) {Console. WriteLine ("OK");} else {Console. WriteLine ("Error");} if (exPtr [0].a1 == exNew.a1 && exPtr [0].a2 == exNew.a2 && exPtr [0].a3 == exNew.a3 && exPtr [0].a4 == exNew.a4 && exPtr [0].a5 == exNew.a5 && exPtr [0].a6 == exNew.a6 && exPtr [0].a7 == exNew.a7 && exPtr [0].a8 == exNew.a8 && exPtr [0].a9 == exNew.a9) {Console. WriteLine ("OK");} else {Console. WriteLine ("Error");}} Explain to me please why on the console deduces following lines: OK Error

2

Re: [unsafe] what that nonsense

Hello, SanyaVB, you wrote: SVB> Explain to me please why on the console deduces following lines: SVB> OK SVB> Error As far as I understand, [StructLayout (LayoutKind. Sequential, Pack = 4)] supervises only layout of fields of structure at  in unmanaged storage, and can 1 not influence layout of its fields in managed storages. Thus, it is impossible to result simply IntPtr the pointer on the unit unmanaged storages to Example *, and to expect that its fields appear in proper places. If it is necessary to set layout of fields which would operate and in managed storages it is necessary to specify explicit offsets: [StructLayout (LayoutKind. Explicit)] public struct Example {[FieldOffset (0x00)] public bool a1; [FieldOffset (0x04)] public bool a2; [FieldOffset (0x08)] public bool a3; [FieldOffset (0x0c)] public bool a4; [FieldOffset (0x10)] public bool a5; [FieldOffset (0x14)] public bool a6; [FieldOffset (0x18)] public bool a7; [FieldOffset (0x1c)] public bool a8; [FieldOffset (0x20)] public bool a9;} 1 There there are any subtleties defining when this attribute influences it, and when there is no. If in them not to press, it is possible to consider simply that generally such warranty is not present.

3

Re: [unsafe] what that nonsense

Hello, nikov, you wrote: N> There there are any subtleties defining when this attribute influences it, and when there is no. If in them not to press, it is possible to consider simply that generally such warranty is not present. And so, for those whom subtleties interest. Here it is written: The common language runtime controls the physical layout of the data fields of a class or structure in managed memory. However, if you want to pass the type to unmanaged code, you can use the StructLayoutAttribute attribute to control the unmanaged layout of the type. Use the attribute with LayoutKind. Sequential to force the members to be laid out sequentially in the order they appear. For blittable types, LayoutKind. Sequential controls both the layout in managed memory and the layout in unmanaged memory. For non-blittable types, it controls the layout when the class or structure is marshaled to unmanaged code, but does not control the layout in managed memory. (For any reason the text italicized, was lost at documentation migration on a new platform) Determination, and also the list blittable types are given here. These are types which have identical idea in managed and unmanaged storages - mainly, primitive types, and also arrays and the structures constructed from them. It is possible to note that bool does not enter into their number (if I am not mistaken, it is connected by that  at copying in managed storage should result any nonzero value bool to canonical true in which one bit that such values correctly behaved with binary operator is installed only). Accordingly, LayoutKind. Sequential for structure Example containing bool, supervises layout of its fields only in unmanaged storages, and it differs from layout of fields in managed storages. The problem dares or usage LayoutKind. Explicit, or changeover bool on byte or int which are blittable types.

4

Re: [unsafe] what that nonsense

Hello, nikov, you wrote: Thanks!