在.NET数组自然对齐? [英] Are arrays in .NET naturally aligned?

查看:79
本文介绍了在.NET数组自然对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问.NET作出任何保证.NET字节数组总是正确对齐?我确实需要此治疗如在不安全的情况下在64位多头字节数组修改的数据块与本地寄存器的大小。

Does .NET make any guarantees that .NET byte arrays are always properly aligned? I do need this to treat e.g. a byte array in unsafe context as longs in x64 to modify chunks of data with the native register size.

但到目前为止,我还没有发现,CLR确实给我任何保证,我的内存访问,然后正确对齐的任何文件。

But so far I have not found any documentation that the CLR does give me any guarantees that my memory access is then properly aligned.

推荐答案

没有。而事实上数组可以是失去定位为x86抖动。尤其是一个问题双重[]长[],垃圾收集器只是提供了一个保证,他们将在4对齐这就解释了双[],这样的阵列将被分配到大对象堆的特殊规则时,有1000个或更多的元素。大大高于对LOH的分配,85000或多个字节的一般规定较少。蕙是根据Windows堆排列的保证,在8对齐这是没有其他的64位抖动的问题。

No. And in fact arrays can be mis-aligned for the x86 jitter. Particularly a problem with double[] and long[], the garbage collector only provides a guarantee that they will be aligned at 4. Which explains the special rule for double[], such an array will be allocated in the Large Object Heap when it has 1000 or more elements. Considerably less than the normal rule for LOH allocations, 85000 or more bytes. The LOH depends on Windows heap alignment guarantees, aligned at 8. This is not otherwise a problem in the x64 jitter.

获得更多具体到你的问题了一下:字节数组永远是一个问题。一个字节始终对齐,无论阵列的开始。 长在非托管code与微软编译器编译的也不是问题,它是4字节,因此总是与默认GC对齐规则开心地对齐。它仍然是64位模式下4字节,因此没有问题存在两种。

Getting a bit more specific to your question: byte arrays can never be a problem. A byte is always aligned, no matter where the array starts. A "long" in unmanaged code compiled with the Microsoft compiler is not a problem either, it is 4 bytes so always aligns happily with the default GC alignment rules. It is still 4 bytes in x64 mode so no problem there either.

这篇关于在.NET数组自然对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆