工程布尔比较等于 true 和 false,为什么?

发布于 2024-10-09 13:57:34 字数 487 浏览 3 评论 0原文

下面的示例编译,但输出相当奇怪:

#include <iostream>
#include <cstring>

struct A
{
    int a;
    char b;
    bool c;
};

int main()
{
    A v;
    std::memset( &v, 0xff, sizeof(v) );

    std::cout << std::boolalpha << ( true == v.c ) << std::endl;
    std::cout << std::boolalpha << ( false == v.c ) << std::endl;
}

输出是:

true
true

有人可以解释为什么吗?

如果重要的话,我正在使用 g++ 4.3.0

The example bellows compiles, but the output is rather strange :

#include <iostream>
#include <cstring>

struct A
{
    int a;
    char b;
    bool c;
};

int main()
{
    A v;
    std::memset( &v, 0xff, sizeof(v) );

    std::cout << std::boolalpha << ( true == v.c ) << std::endl;
    std::cout << std::boolalpha << ( false == v.c ) << std::endl;
}

the output is :

true
true

Can someone explains why?

If it matters, I am using g++ 4.3.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

余生共白头 2024-10-16 13:57:35

覆盖 v 使用的内存位置的结果是未定义行为
根据标准,一切都可能发生(包括你的电脑飞走和吃早餐)。

The result of overwriting memory location used by v is undefined behaviour.
Everything may happen, according to the standard (including your computer flying off and eating your breakfast).

橘虞初梦 2024-10-16 13:57:35

其内存设置为非一或零的值的布尔值具有未定义的行为。

A boolean value whose memory is set to a value that is not one or zero has undefined behaviour.

淡笑忘祈一世凡恋 2024-10-16 13:57:35

我想我找到了答案。 3.9.1-6 说:

bool 类型的值为 true 或
false.42) [注:没有签名,
无符号、短或长布尔类型或
价值观。 ] 如下所述,bool
值表现为整数类型。
bool 类型的值参与
积分促销 (4.5)。

注释 42 说:

42) 使用布尔值的方式
本国际所描述的
标准为“未定义”,例如
检查一个的价值
未初始化的自动变量,
可能会导致它表现得好像
不真实也不虚假。

I thing I found the answer. 3.9.1-6 says :

Values of type bool are either true or
false.42) [Note: there are no signed,
unsigned, short, or long bool types or
values. ] As described below, bool
values behave as integral types.
Values of type bool participate in
integral promotions (4.5).

Where the note 42 says :

42) Using a bool value in ways
described by this International
Standard as ‘‘undefined,’’ such as by
examining the value of an
uninitialized automatic variable,
might cause it to behave as if it is
neither true nor false.

戒ㄋ 2024-10-16 13:57:35

我似乎在标准中找不到任何内容来表明为什么会发生这种情况(很可能是我的错)——这确实包括 7vies 提供的参考,但它本身并没有多大帮助。这绝对是未定义的行为,但我无法解释 OP 观察到的具体行为。

作为一个实际问题,我非常惊讶输出是

true
true

使用 VS2010,输出更容易解释:

false
false

在后一种情况下,发生的情况是:

  • 与布尔 true 的比较由编译器测试是否与 0x01 相等,并且由于 0xff != 0x01 结果为 false
  • 与布尔值 false 的比较也是如此,只是比较的值现在是 0x00

我想不出任何实现细节会导致 false 在解释为 bool 时与值 0xff 进行比较。有人对此有什么想法吗?

I can't seem to find anything in the standard that indicates why this would happen (most possibly my fault here) -- this does include the reference provided by 7vies, which is not in itself very helpful. It is definitely undefined behavior, but I can't explain the specific behavior that is observed by the OP.

As a practical matter, I 'm very surprised that the output is

true
true

Using VS2010, the output is the much more easy to explain:

false
false

In this latter case, what happens is:

  • comparisons to boolean true are implemented by the compiler as tests for equality to 0x01, and since 0xff != 0x01 the result is false.
  • same goes for comparisons to boolean false, only the value compared with is now 0x00.

I can't think of any implementation detail that would cause false to compared equal to the value 0xff when interpreted as bool. Anyone have any ideas about that?

会发光的星星闪亮亮i 2024-10-16 13:57:34

在 C++ 标准第 3.9.1 节“基本类型”中找到了这一点(注意神奇的脚注 42):

6. Values of type bool are either true or false. 42)

42) 以本国际标准描述为“未定义”的方式使用布尔值,例如通过检查未初始化的自动变量的值,可能会导致其表现得好像它既不是 true 也不是 false。 /p>

这对我来说并不完全清楚,但似乎回答了这个问题。

Found this in the C++ standard, section 3.9.1 "Fundamental types" (note the magic footnote 42):

6. Values of type bool are either true or false. 42)

42) Using a bool value in ways described by this International Standard as ‘‘undefined,’’ such as by examining the value of an uninitialized automatic variable, might cause it to behave as if it is neither true nor false.

This is not perfectly clear for me, but seems to answer the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文