位段:修订间差异

求闻百科,共笔求闻
添加的内容 删除的内容
(机器人:自动替换模板(来源模板);移除无用的模板参数。)
(机器人:清理不当的来源、移除无用的模板参数)
 

(未显示另一用户的1个中间版本)

第34行: 第34行:
如果位域的定义没有给出标识符名字,那么这是'''无名位域''',无法被初始化。<ref>ISO 14882 C++14 standard §9.6.2</ref>无名位域用于填充(padding)内存布局。只有无名位域的比特数可以为0。这种占0比特的无名位域,用于强迫下一个位域在内存分配边界对齐。
如果位域的定义没有给出标识符名字,那么这是'''无名位域''',无法被初始化。<ref>ISO 14882 C++14 standard §9.6.2</ref>无名位域用于填充(padding)内存布局。只有无名位域的比特数可以为0。这种占0比特的无名位域,用于强迫下一个位域在内存分配边界对齐。
== 实现 ==
== 实现 ==
通常在[[大端序]]系统(如[[PowerPC]]),安排位域从最重要字节(most-significant byte)到最不重要字节(least-significant byte),在一个字节内部从最重要位(most-significant bit)到最不重要位(least-significant bit);而在[[小端序]]系统(如[[x86]]),安排位域从最不重要字节(least-significant byte)到最重要字节(most-significant byte),在一个字节内部从最不重要位(least-significant bit)到最重要位(most-significant bit)。<ref>{{Cite web |url=http://mjfrazer.org/mjfrazer/bitfields/ |title=How Endianness Effects Bitfield Packing |accessdate=2017-03-15 |||}}</ref>共同遵从的原则是内存字节地址从低到高,内存内部的{{tsl|en|bit numbering|比特编号}}从低到高。
通常在[[大端序]]系统(如[[PowerPC]]),安排位域从最重要字节(most-significant byte)到最不重要字节(least-significant byte),在一个字节内部从最重要位(most-significant bit)到最不重要位(least-significant bit);而在[[小端序]]系统(如[[x86]]),安排位域从最不重要字节(least-significant byte)到最重要字节(most-significant byte),在一个字节内部从最不重要位(least-significant bit)到最重要位(most-significant bit)。<ref>{{Cite web |url=http://mjfrazer.org/mjfrazer/bitfields/ |title=How Endianness Effects Bitfield Packing |accessdate=2017-03-15 }}</ref>共同遵从的原则是内存字节地址从低到高,内存内部的{{tsl|en|bit numbering|比特编号}}从低到高。
=== Microsoft Visual C++实现 ===
=== Microsoft Visual C++实现 ===
在一个整数(integer)内的位域从最不重要位(least-significant)向最重要位(most-significant)依次分配。
在一个整数(integer)内的位域从最不重要位(least-significant)向最重要位(most-significant)依次分配。


相邻的两个位域如果基类型(underlying type)的长度相同,在后的位域适合当前内存分配单元且没有跨内存分配边界,那么这两个位域分配到同一个(1、2或4字节的)分配单元。<ref>{{Cite web |url=https://msdn.microsoft.com/EN-US/library/1d48zaa8(v=VS.140,d=hv.2).aspx |title=&#91;&#91;MSDN&#93;&#93; "Storage and Alignment of Structures" wrote: Adjacent bit fields are packed into the same 1-, 2-, or 4-byte allocation unit if the integral types are the same size and if the next bit field fits into the current allocation unit without crossing the boundary imposed by the common alignment requirements of the bit fields. |accessdate=2017-03-15 |||}}</ref>这可以通俗理解为:具有相同的基类型(underlying type)长度的相邻位域尽量装入基类型的同一个对象,如果装得下的话。
相邻的两个位域如果基类型(underlying type)的长度相同,在后的位域适合当前内存分配单元且没有跨内存分配边界,那么这两个位域分配到同一个(1、2或4字节的)分配单元。<ref>{{Cite web |url=https://msdn.microsoft.com/EN-US/library/1d48zaa8(v=VS.140,d=hv.2).aspx |title=&#91;&#91;MSDN&#93;&#93; "Storage and Alignment of Structures" wrote: Adjacent bit fields are packed into the same 1-, 2-, or 4-byte allocation unit if the integral types are the same size and if the next bit field fits into the current allocation unit without crossing the boundary imposed by the common alignment requirements of the bit fields. |accessdate=2017-03-15 }}</ref>这可以通俗理解为:具有相同的基类型(underlying type)长度的相邻位域尽量装入基类型的同一个对象,如果装得下的话。


== 参考文献 ==
== 参考文献 ==
{{reflist}}
{{reflist}}

[[Category:位数据结构]]
[[Category:位数据结构]]
[[Category:带有C代码示例的条目]]
[[Category:带有C代码示例的条目]]