奇怪的布尔值 [英] Strange bool values

查看:119
本文介绍了奇怪的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类,它拥有一个私人布尔成员变量调用阳性。我想检查,看看是否我的课,我将它的数量都是正面,所以我写了前pression

I have this class which has a private bool member variable call positive. I want to check to see if both my class and the number I am adding to it are both positive so I wrote the expression

positive == (n > 0) //n is of type int

但由于某些原因,当双方都积极评价此为假。然后我做了一个正面cout和它有一个204的价值!我虽然布尔仅持有1或0。这将是为什么这是评估为假,但是这是为什么?我如何解决它?我使用Visual Studio 2013。

But for some reason when both are positive this evaluates to false. I then did a cout on positive and it has a value of 204! I though a boolean only holds 1 or 0. That would be why this is evaluating to false but why is that and how do I fix it? I am using Visual Studio 2013.

只是为了澄清应==因为我想知道,如果标志是一样的。所以我想,如果都是阴性或两者都积极它是真实的。我想我解决了是在一个特定的测试用例正面没有被初始化的问题。但仍为什么即使未初始化一个bool变量包含一个值较大,1?

Just to clarify it should be == because I want to know if the sign is the same. So I want it to be true if both are negative or both are positive. I think I solved the problem which was that positive in one particular test case wasn't being initialized. But still why would a bool variable even if uninitialized contain a value larger and 1?

推荐答案

按照标准,得到一个未初始化的变量的值是不确定的行为。
由于布尔的实际大小可以超过一位,含有垃圾一个布尔变量可以甚至认为像C ++地狱254.欢迎错误值!

As per the standard, getting the value of an uninitialized variable is undefined behavior. Since the actual size of a bool can be more than one bit, a bool variable containing rubbish can even hold bad values like 254. Welcome in the C++ hell!

此外,由于(N> 0)的计算结果为1,254 == 1为假。

And since (n > 0) evaluates to 1, 254 == 1 is false.

该错误很可能是由于这一事实,即从来没有因为包含它进入存在对象的一个​​有意义的值。

The bug is most likely due to the fact that positive never got a meaningful value since the object that contains it come into existence.

搜索一个 =正面的东西到code和看它是否被执行至少一次使用它之前。

Search for a positive = something into your code and see if it is executed at least once before you use it.

康拉德评论后编辑

问题很可能是由于这一事实,即==是INT-S之间实施,而不是bool的-S依靠布尔对INT推广假设BOOL使用0虚假的执行和1代表真(和因为和放大器;&安培;和||之间INT-S假设0假,非零真正的,布尔对INT推广可以是一个标识)

The problem is most likely due to the fact that == is implemented between int-s and not bool-s relying on a bool-to-int promotion assuming the implementation of bool use 0 for false and 1 for true (and since && and || between int-s assume 0 for false and non-zero for true, bool-to-int promotion can be an identity).

这可以被看作是一种不公平编译,但在 - 由于整数的空间,有一个假的,如果 ==操作符trues-的bilions(布尔,布尔)施,那里有更多的机会未初始化错误沃尔德一直没有发现。

This can be seen as a "compiler unfairness", but -since in the space of integer there is one false and bilions of trues- if operator==(bool,bool) was applied, there where more chance the uninitialized bug wold have been not discovered.

在算术运算(而不是逻辑的)治疗BOOL为int使不良布尔值出来,从而显示出错误。

Treating bool as int in the arithmetic operations (not logic ones) makes bad-bool values to come out, thus revealing the bug.

当然,因为它背后有一个UB,这一切都在一个实现选择。它是int值阅读本的自由,觉得我尝试出售一个bug为特征。

Of course, since behind it there is an UB, it's all in an implementation choice. And it is int the freedom of each reading this to think I'm try to sell a bug as a feature.

这篇关于奇怪的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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