bool在内部如何存储? [英] How is bool internally stored?
本文介绍了bool在内部如何存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我假设bool变量默认情况下可以容纳true或false值,但是当我运行此代码段时,我知道默认值是未定义的.
我尝试打印bTest值并在每次运行时获得不同的输出.这是否意味着bool等同于未签名的char?
int main() { bool bTest; if(bTest != true && bTest != false) printf("bool default value is undefined - %d",bTest); return 0;
)
解决方案
在内部,布尔值以字节存储,并且(与其他任何未初始化的变量一样)它将具有随机"值-实际上这是十六进制CC-与true
(1)或false
(0)的值不匹配
虽然仅使用一个位来存储布尔变量会节省空间,但使用字节则要更节省处理器:位操作比字节需要更多的组装步骤!全部,
谢谢您的答复,所以仅需总结一下-bool可以在为其分配的内存空间中使用该值,在调试模式下,它会给出警告-该变量未初始化,在发行版中,我们必须确保我们正在使用分配给它的值.
Hi,
I assumed that bool variable can hold either true or false values by default,but when I ran this code snippet I got to know that default value is undefined.
I tried to print the bTest value and got different outputs at each run.Does that mean that bool is equivalent to unsigned char?
int main() { bool bTest; if(bTest != true && bTest != false) printf("bool default value is undefined - %d",bTest); return 0;
)
解决方案
Internally, a bool is stored as a byte, and (like any other uninitialized variable) it will have a "random" value - in practice this is hex CC - which does not match the value oftrue
(1) orfalse
(0)
While it would be more space efficient to use only a single bit to store a bool, it is far more processor efficient to use a byte: bit operations need more assembly steps than byte!
Hi all,
thanks for your replies,so just to conclude - bool can have the value present in the memory space it is assigned with,in debug mode it gives a warning - that variable is uninitialized ,in release we have to make sure we are using it assigned.
这篇关于bool在内部如何存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文