C#与C++进行字段内存对齐

通过预留字段来补齐内存分配。在实际项目中采用这种方法较多,即保证了长度一致,也为以后的扩展提供了容错的可能性。

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.SequentialLayoutKind.ExplicitLayoutKind.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); } ```

相关推荐
重生之后端学习2 分钟前
苍穹外卖-day03
java·开发语言·数据库·spring boot·mysql·spring·tomcat
程序员阿超的博客9 分钟前
【安全篇】金刚不坏之身:整合 Spring Security + JWT 实现无状态认证与授权
java·spring boot·安全·spring
异常君30 分钟前
Java 应用中构建 Elasticsearch 多层次缓存:提升查询效率的实战方案
java·elasticsearch·架构
nenchoumi311936 分钟前
UE5 学习系列(一)创建一个游戏工程
c++·学习·游戏·ue5
泽韦德37 分钟前
【Redis】笔记|第10节|京东HotKey实现多级缓存架构
redis·笔记·缓存
橘子编程1 小时前
Maven从入门到精通指南
java·maven
wodownload21 小时前
CS003-2-2-perfermance
java·开发语言·jvm
想用offer打牌1 小时前
面试官拷打我线程池,我这样回答😗
java·后端·面试
真的很上进1 小时前
2025最全TS手写题之partial/Omit/Pick/Exclude/Readonly/Required
java·前端·vue.js·python·算法·react·html5
百锦再1 小时前
.Net 优秀框架 ABP全面详解
microsoft·.net·web·blazor·abp·razor