C - 使用 memset 设置未定义的 _Bool 后读取 _Bool 是否已定义,实现是否已定义?

发布于 2025-01-15 14:10:33 字数 605 浏览 2 评论 0原文

在 ISO 标准 C 中,我的理解是,没有什么可以真正确定 _Bool 的表示形式,但它确实说:

  • “_Bool 足够大,可以容纳值 0 和 1”
  • “当任何标量值转换为 _Bool 时,如果值比较等于0,则结果为0; 否则,结果为 1”
  • “_Bool 中的位数至少为 CHAR_BIT,_Bool 的宽度只能是 1 位”

我当时在想(以及从其他相关答案), false 的表示实际上不必为 0 (即使在几乎所有实现中,都是如此)。那么如果您将 _Bool 设置为 0,然后以某种方式使用它,会发生什么情况?这是未定义的行为(默认情况下,因为它没有在标准中定义)或者实现定义的行为?这似乎很重要(以我的理解),因为在前一种情况下,它不是一个定义良好的 C 程序,例如,这个未定义的行为可以有 0 以外的表示吗?

#include <stdbool.h>

//...

bool x = true;

memset(&x, 0, sizeof(bool));

if(x == true)
{
    printf("Zero is true!");
}
else
{
    printf("zero is false!");
}

In ISO standard C, my understanding is that there is nothing that actually nails down the the representation of a _Bool, but it does say:

  • "_Bool is large enough to hold the values 0 and 1"
  • "When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0;
    otherwise, the result is 1"
  • "number of bits in a _Bool is atleast CHAR_BIT the width of a _Bool can be just 1 bit"

I am thinking then (and from other related answers), that the representation of false need not actually be 0 (even though in nearly all implementations, it is). So what happens if you memset a _Bool to 0, then use it somehow? Is this undefined behavior (by default because it is not defined in the standard) or implementation defined behavior? This seems to matter (in my understanding) because in the former case, it's not a well defined C program, in the latter it is. For example is this undefined behavior? Can false have a representation other than 0?

#include <stdbool.h>

//...

bool x = true;

memset(&x, 0, sizeof(bool));

if(x == true)
{
    printf("Zero is true!");
}
else
{
    printf("zero is false!");
}

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

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

发布评论

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

评论(1

冧九 2025-01-22 14:10:33

_Bool 是无符号整数类型。它至少可以表示值 0 和 1。请注意,没有单独的 truefalse 值。 stdbool.h 中的宏 true 扩展为常量 1,宏 false 扩展为常量 0 7.18。因此,x == truex == 1 相同。

无符号整数类型中有两种位:值位和填充位 6.2.6.2p1。您对 memset 的调用将所有位(值和填充)设置为零。

对于任何整数类型,所有位都为零的对象表示应是该类型中值零的表示 6.2.6.2p5

因此,所示的程序片段没有可见的未定义、未指定或实现定义的行为。一个合理完成的程序应该打印zero is false

_Bool is an unsigned integer type. It can represent at least values 0 and 1. Note there are no separate true and false values. The macro true in stdbool.h expands to the constant 1, and the macro false to the constant 0 7.18. So x == true is the same as x == 1.

There are two kinds of bits in an unsigned integer type: value bits and padding bits 6.2.6.2p1. Your invocation of memset sets all bits (value and padding) to zero.

For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type 6.2.6.2p5.

Thus, the program fragment as shown has no visible undefined, unspecified or implementation-defined behaviour. A reasonably completed program shall print zero is false.

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