数组 C++ 中每个布尔值 1 位

发布于 2024-09-30 02:05:28 字数 119 浏览 8 评论 0原文

bool fp[81];

根据我的理解, fp 应该使用 ceil(81/8) 字节,因为它是连续的。

我说得对吗?

我怎样才能证明这一点?

bool fp[81];

From my understanding fp should use ceil(81/8) bytes because it is in succession.

Am I correct?

How can I prove this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

扭转时空 2024-10-07 02:05:28

不,缓冲区的大小是实现定义的,根据标准的引用:

$5.3.3/1 - “sizeof 运算符生成其操作数的对象表示形式中的字节数。操作数可以是未求值的表达式,也可以是带括号的类型 ID。sizeof 运算符不得应用于具有函数或不完整类型的表达式,或者应用于声明其所有枚举器之前的枚举类型,或者应用于此类类型的带括号的名称,或者应用于指定的左值
一个位域。 sizeof(char)、sizeof(signed char) 和 sizeof(unsigned char) 均为 1;这
sizeof 应用于任何其他基本类型 (3.9.1) 的结果是实现定义的。 [注:特别是,sizeof(bool) 和 sizeof(wchar_t) 是实现定义的。69)] [注:字节的定义参见 1.7,对象表示的定义参见 3.9。]

因此,您可以预期的大小是 81 * X,其中 X 是 bool 的大小,这是实现定义的。

No, the sizeof your buffer is implementation defined, as per this quote from the Standard:

$5.3.3/1 - "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 particular, 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.]

Therefore the size you can expect is 81 * X, where X is the size of bool, which is implementation defined.

花开半夏魅人心 2024-10-07 02:05:28

不,它的 81*sizeof(bool) 很可能是 81 字节

no, its 81*sizeof(bool) which is most likely 81 bytes

一腔孤↑勇 2024-10-07 02:05:28

您可以使用 sizeof 找出任何对象或类型使用的存储空间:

int main() {
  bool fp[81];
  cout << sizeof fp << '\n';
  cout << sizeof(bool[81]) << '\n';
  return 0;
}

You can find out the storage used by any object or type with sizeof:

int main() {
  bool fp[81];
  cout << sizeof fp << '\n';
  cout << sizeof(bool[81]) << '\n';
  return 0;
}
我的黑色迷你裙 2024-10-07 02:05:28

你可以使用 sizeof(fp) 检查它的大小,在我的例子中给出 81

you can check its size using sizeof(fp) which in my case gives 81

街角卖回忆 2024-10-07 02:05:28

不,每个布尔值通常单独存储(通常,具体取决于您的计算机,8 位)。占用的内存至少为 81 字节。

No, each bool is usually stored separately (usually, depending on your computer, 8-bits). The memory occupied would be a minimum of 81 bytes.

因为看清所以看轻 2024-10-07 02:05:28

如果您想确保每个位都被视为一个位,而不是使用一个字节表示整个值,请使用位集:

#include <bitset>
using namespace std;
#define SIZE 1000;
int main()
{
bitset<SIZE> bit_set; // unfortunately the size of a bitset is determined at compile time
bit_set.flip();
bit_set[232] = true;
}

您必须了解这是处理器及其指令的非常低级的内存约束,因为它们被设计为支持位字,而不是位。
不过,为此目的添加一些指令本来是一件好事,因为 bitset 所做的只是位移位......

我真的需要学习 x86 汇编。

Use a bitset if you want to be sure each bit will be considered as a bit instead of using a byte for a whole value:

#include <bitset>
using namespace std;
#define SIZE 1000;
int main()
{
bitset<SIZE> bit_set; // unfortunately the size of a bitset is determined at compile time
bit_set.flip();
bit_set[232] = true;
}

You have to understand this is a very low level memory constraint of processors and their instructions, as they are designed to support words of bits, not bits.
It would have been a good thing that some instructions would have been added for this purpose though, since the thing bitset does is just bit shifting...

I really need to learn x86 assembly.

少女的英雄梦 2024-10-07 02:05:28

不,bool 是 8 位。使用vector(专门的位打包向量)或bitset

No, a bool is 8 bits. Use vector<bool> (a specialized bit-packed vector) or bitset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文