这是"字对齐"在.NET?这是、QUOT、NET

2023-09-05 03:21:00 作者:心动九十九次

这里在这篇文章中,的埃里克利珀的告诉引用必须是字对齐的.NET 。可能有人请解释一下是什么意思吗? - 在互联网上搜索,但没有找到对此的任何有用的资源

下面是完整的句子:

  制表位与文字对齐的秘密

您永远要放在旁边的一个参考字节的结构,如果你   都不可能避免;这样的结构会浪费三至七个字节   自动。参考文献必须是字对齐的.NET。

解决方案

我觉得它做的数据的大小和它的结构内部的对齐和填充。

据介绍这里(重点煤矿):

  

最重要的是,编译器将添加填充字节为对齐内的结构数据。你可以明确地控制填充,但是请注意,一些处理器,如果你使用未对齐的数据抛出一个异常,而这为.NET精简用户一个更复杂的问题。

另外看看。显然,字大小是

  

的比特数,一个CPU可以在同一时间处理

所以,调整你的数据就意味着把它们放在一起以这种方式没有因填充和您采取的考虑,可以读取特定平台上的内存的大小,内存之间丢失了。

Here in this post, Eric Lippert told References have to be word-aligned in .NET. Could someone please explain what does he mean here? - searched on the internet but couldn't find any helpful resource regarding this.

Here is the full sentence:

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.

解决方案

I think it has to do with the size of the data and the alignment and padding of it inside the struct.

It is explained here (emphasis mine):

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.

Also take a look here. Apparently, the word size is

the number of bits that a CPU can process at one time.

So aligning your data will mean to put them together in that way there is no memory lost between them due to padding and that you take in consideration the size of the memory that can be read on that specific platform.