为什么存储开销会在C#数据类型中产生浪费? [英] Why storage overhead generates waste in C# data types?
问题描述
在子主题存储开销(在本章中)- C#5.0简而言之本书中,有这样一条一般性注释:
In subtopic Storage Overhead (on Chapter) -C# 5.0 in a Nutshell book- there is this general note that says:
现在,我想知道为什么结构 A 中的字段会浪费空间?或者,整个注释对作者有何意义?
Now, I'm wondering why the fields in struct A generates a waste of space? Or, what is the author's point with the entire note?
推荐答案
每个 byte
字段占用1个字节,而每个 long
字段占用8个字节.这意味着,尽管 b
可以放置在内存中的任何位置,但 l
仍需要放置在8的倍数的地址处.不能将其放置在地址中> 0
,因为它已经被 b
占用;因此,必须将其放置在8的下一个可用倍数下,即 8
,从而浪费了7个字节的中间空间.
Each byte
field occupies 1 byte, whilst each long
field occupies 8 bytes. This means that, whilst b
could be placed anywhere in memory, l
needs to be placed at an address that is a multiple of 8. It cannot be placed at address 0
since that is already occupied by b
; thus, it must be placed at the next available multiple of 8, which is 8
, causing the 7 bytes of intervening space to be wasted.
---------------------------------------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---------------------------------------------------------------------------------
<--b-> <------------------l-------------------->
<--------------waste------------->
这篇关于为什么存储开销会在C#数据类型中产生浪费?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!