bool在内部如何存储?

编程入门 行业动态 更新时间:2024-10-25 00:35:32
本文介绍了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 of true (1) or false (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在内部如何存储?

本文发布于:2023-11-25 15:46:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在内部   bool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!