English 中文(简体)
Bools的压缩结构的大小
原标题:Size of a Packed Structure of Bools
  • 时间:2010-10-27 17:03:45
  •  标签:
  • c++
  • memory

如果1个布尔是1字节[8位],那么4个布尔的压缩结构是32位还是4位?pack指令删除了对齐要求,但它会使布尔集更有效率[内存方面]吗?

最佳回答

对即使是布尔值的压缩结构,每个布尔值也至少使用8位。除非您使用位字段

问题回答

4<code>bool</code>s。

每个<code>bool</code>都需要一个唯一的地址(因为您可以使用<code>的bool<-code>的地址)。如果您使用一个位字段,您可以将大小减小到1个bool,但您将无法获得单个位字段的地址。

bool的大小可能因操作系统和语言而异。我看到它是一个字节、一个单词和一个int(反过来也可以是任何东西)。但是,如果sizeof(bool)是1,那么bool的压缩结构将是4(字节)(因此是32位)

与其打乱包装和对齐,为什么不使用:

std::vector<bool>

发件人:http://www.cplusplus.com/reference/stl/vector/

它在内部被优化(或应该被优化)为一个位字段。试试看,你会发现它使用的内存与每个值的单个位是一致的。

否则,您可以始终滚动自己的库或使用有限的FD_SET宏。

这是一个已定义的实现。该标准没有定义布尔值的大小。

编辑

这来自标准:

5.3.3尺寸

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in par- ticular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69) ] [Note: See 1.7 for the definition of byte and 3.9 for the definition of object representation. ]





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签