如何是BOOL重新$ P $内存psented? [英] How is a bool represented in memory?
问题描述
由于在文档中已经讨论过 中,一个布尔
数据类型占用内存至少为一个字节。类似的问题,有人问这之前(如何一个布尔类型变量存储在内存中?(C ++)),但这种讨论和文档似乎只是讨论一个布尔数据类型占用空间的大小,而不是实际发生的内存的当我这样做:
布尔B =真;
那么,是什么实际上是在内存发生?发生未在存储该信息中使用的7位是什么?是否标准prescribe行为呢?
他们是不确定的?还是因为有人在C ++总部只有做到这一点:
枚举布尔:CHAR
{
假= 0,
真= 1
};
标准规定布尔
值表现为整型,但它没有指定具体的再$在内存不足p $ psentation:
的类型的值布尔
或者是真正
或假
。如下所述,布尔
值表现为整型,类型的值布尔
参加积分促销的<子>〜C ++ 03 3.9.1§6
的一个整数类型同义词是整型。整型的重新presentations应由使用纯二进制记数系统的定义值的<子>〜C ++ 3.9 03 .1§7
然而,它定义了从布尔
的积分提升到 INT
:
类型的右值布尔
可转换为类型的右值 INT
与假
成为零和真正
成为一体。这些转换称为积分促销活动。的 <子>〜C ++ 4.5 03§4-5
,以及从其他类型转换布尔
:
零值,空指针值或空成员指针值转换为假
;其它任何值转换为真正
的 <子>〜C ++ 03 4.12§1
As already discussed in the docs, a bool
data type occupies at least a byte of memory. A similar question was asked on SO before (How a bool type variable is stored in memory? (C++)), but this discussion and the documentation only seem to discuss the amount of space occupied by a boolean data type, not what actually happens in memory when I do this:
bool b = true;
So what does actually happen in memory? What happens to the 7 bits that are not used in storing this information? Does the standard prescribe behavior for this?
Are they undefined? Or did someone at C++ headquarters just do this:
enum bool : char
{
false = 0,
true = 1
};
Standard states that bool
values behave as integral types, yet it doesn't specify their concrete representation in memory:
"Values of type bool
are either true
or false
. As described below, bool
values behave as integral types. Values of type bool
participate in integral promotions" ~ C++03 3.9.1 §6
"A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system" ~ C++03 3.9.1 §7
Yet it defines the integral promotion from bool
to int
:
"An rvalue of type bool
can be converted to an rvalue of type int
, with false
becoming zero and true
becoming one. These conversions are called integral promotions." ~ C++03 4.5 §4-5
as well as conversion from other types to bool
:
"A zero value, null pointer value, or null member pointer value is converted to false
; any other value is converted to true
." ~ C++03 4.12 §1
这篇关于如何是BOOL重新$ P $内存psented?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!