.net - .NET 中的 "word-aligned"是什么?

标签 .net struct

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/

相关文章:

c# - 您能解释 .NET 运行时中的这种奇怪的崩溃吗?

c - 将结构指针元素传递给 FIFO - C

c - 将命名结构作为匿名结构嵌入到另一个结构中

c# - 将 foreach 转换为 linq 表达式

c# - 此处是否需要 GC.KeepAlive,或者我可以依靠局部变量和参数来保持对象的事件状态吗?

c# - 如何在 .NET 中使用 GDI+ 中没有的 Windows GDI 方法?

c++ - 使用对象指针时访问冲突读取

c# - Windows Workflow 4.5 延迟事件未持续

map - 类型映射的 golang 匿名字段

c++ - struct stat c++ 不在不同的计算机上编译