C # controls data alignment

In C ++, you can use declarator __declspec( align( # ) ) to control the alignment of user data. How to do it for C #. I have two procedures written in Assembler in my dll. Arguments for procedures (two arrays) must be aligned by 16 bytes. For C ++, it works great.

I just used ads

 __declspec( align( 16 ) ) double a[2]={10.2,10.6}; 
+7
source share
1 answer

If you are looking for a managed interface (transferring data between C # /. NET and C / C ++ / assembler), you should use a combination of StructLayout and the FieldOffset attribute:

 [StructLayout(LayoutKind.Explicit, Pack = 16)] public class MyDataClass { [FieldOffset(0)] double[] a; } 

According to MSDN:

The System.Runtime.InteropServices.StructLayoutAttribute.Pack field determines the memory alignment of the data fields of the target object.

http://support.microsoft.com/kb/922785

+8
source

All Articles