Here在这篇文章中,Eric Lippert告诉 引用必须在 .NET 中字对齐
。有人可以解释一下他在这里的意思吗? - 在互联网上搜索,但找不到任何与此相关的有用资源。
完整句子如下:
You never want to put a byte next to a reference in a struct if you can possibly avoid it; such a struct will waste three to seven bytes automatically. References have to be word-aligned in .NET.
最佳答案
我认为这与数据的大小以及结构内数据的对齐和填充有关。
已说明here (强调我的):
What is important is that the compiler will add “padding” bytes to align the data within a struct. You can control the padding explicitly, but notice that some processors throw an exception if you use data that isn’t aligned, and this creates a more complicated problem for .NET Compact users.
也看看here 。显然,字长是
the number of bits that a CPU can process at one time.
因此,对齐数据意味着将它们放在一起,这样它们之间就不会因填充而丢失内存,并且您会考虑在该特定平台上可以读取的内存大小。
关于.net - .NET 中的 "word-aligned"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862171/