通过预留字段来补齐内存分配。在实际项目中采用这种方法较多,即保证了长度一致,也为以后的扩展提供了容错的可能性。
csharp
unsafe struct StructSequential
{
public fixed float x[8];
public fixed float y[8];
public fixed float z[8];
public fixed float time[8];
public fixed float radius[8];
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public float[] reserved;
}
或者
csharp
public unsafe struct RTCPointQuery8
{
public fixed float x[8];
public fixed float y[8];
public fixed float z[8];
public fixed float time[8];
public fixed float radius[8];
}
Span<byte> stack = stackalloc byte[sizeof(RTCPointQuery8) + 32];
ref var test = ref StackAllocAligned<RTCPointQuery8>(stack, 32);
static unsafe ref T StackAllocAligned<T>(Span<byte> stack, nuint alignment) where T : unmanaged
{
return ref Unsafe.AsRef<T>((void*)(((nint)Unsafe.AsPointer(ref MemoryMarshal.GetReference(stack))
+ ((nint)alignment - 1)) & ~(nint)(alignment - 1)));
}
C#内存对齐有三种方式 ,分别是:LayoutKind.Sequential
、LayoutKind.Explicit
、LayoutKind.Auto
。
LayoutKind.Sequential
顺序对齐,这是默认的内存对齐方式。会按照定义的struct中最大成员的长度进行内存对齐,使用这种对齐方式容易造成内存空间浪费。这种方式常用于和非托管代码交互的情形,CLR对struct的Layout处理方法与C/C++中默认的处理方式相同,都是按照结构体中占用空间最大的成员进行内存对齐。
csharp
[StructLayout(LayoutKind.Sequential)]
struct StructSequential
{
public int A;
public char B;
}
public static void Start()
{
StructSequential structSequential = new StructSequential();
Debug.WriteLine("A的长度为:{0}", Marshal.SizeOf(structSequential.A));
Debug.WriteLine("B的长度为:{0}", Marshal.SizeOf(structSequential.B));
Debug.WriteLine("structSequential的长度为:{0}", Marshal.SizeOf(structSequential));
}
//A的长度为:4
//B的长度为:1
//structSequential的长度为:8
如果我们正在创建一个与非托管代码没有任何交互的struct类型,我们也希望改变C#编译器的这种默认规则,因此,除了Sequential外,还有Explicit和Auto。
除此之外,你还可以通过设置StructLayout.Pack的值来达到内存大小分配一致,但这种方法可能会因硬件性能约束或者其他的问题。
csharp
[StructLayout(LayoutKind.Sequential, Pack = 32)]
unsafe struct StructSequential
{
public fixed float x[8];
public fixed float y[8];
public fixed float z[8];
public fixed float time[8];
public fixed float radius[8];
}
LayoutKind.Explicit
精确对齐,CLR会按照程序的设置对内存进行精确对齐。传入LayoutKind.Explicit,可以使字段按照我们设定的FieldOffset设置字段排序方式,CLR不对结构体进行内存对齐;但是,这种方式也是有缺陷的,如果设置错误,后果将比较严重。
csharp
[StructLayout(LayoutKind.Explicit)]
struct StructSequential
{
[FieldOffset(0)]
public int A;
[FieldOffset(0)]
public char B;
}
public static void Start()
{
StructSequential structSequential = new StructSequential();
Debug.WriteLine("A的长度为:{0}", Marshal.SizeOf(structSequential.A));
Debug.WriteLine("B的长度为:{0}", Marshal.SizeOf(structSequential.B));
Debug.WriteLine("structSequential的长度为:{0}", Marshal.SizeOf(structSequential));
structSequential.A = 0;
structSequential.B = '0';
Debug.WriteLine("structSequential.A: {0}", structSequential.A);
Debug.WriteLine("structSequential.B: {0}", structSequential.B);
}
//A的长度为:4
//B的长度为:1
//structSequential的长度为:4
//structSequential.A: 48
//structSequential.B: 0
[FieldOffset(0)]表示内存不会有任何偏移,这种情形下,同一字节中存储了两条数据,当其中一条改变时,另一条也随之而改变。
LayoutKind.Auto
自动对齐。CLR会调整struct中成员的顺序来自动对齐,使实例仅可能占用更少的内存,同时会进行一定字节的内存对齐。 按照自己选择的最优方式对实例中的字段进行排列。
csharp
[StructLayout(LayoutKind.Auto)]
struct StructSequential
{
public int A;
public char B;
}
public static void Start()
{
StructSequential structSequential = new StructSequential();
Debug.WriteLine("A的长度为:{0}", Marshal.SizeOf(structSequential.A));
Debug.WriteLine("B的长度为:{0}", Marshal.SizeOf(structSequential.B));
//Debug.WriteLine("structSequential的长度为:{0}", Marshal.SizeOf(structSequential));
unsafe
{
Debug.WriteLine("structSequential的长度为:{0}", sizeof(StructSequential));
}
structSequential.A = 0;
structSequential.B = 'a';
Debug.WriteLine("structSequential.A: {0}", structSequential.A);
Debug.WriteLine("structSequential.B: {0}", structSequential.B);
}