如何是BOOL重新$ P $内存psented? [英] How is a bool represented in memory?

查看:109
本文介绍了如何是BOOL重新$ P $内存psented?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在文档中已经讨论过 中,一个布尔数据类型占用内存至少为一个字节。类似的问题,有人问这之前(如何一个布尔类型变量存储在内存中?(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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆