c++ - 将int的所有字节都设置为(unsigned char)0,保证代表零?

标签 c++ c++11 language-lawyer

This is not a matter of recommended practise (nor undefined behavior), but about what the c++-standard actually guarantees in the matter of turning all bytes of an integer type to the value of (unsigned char)0.


问题

在下面的片段中,if-statement 使用的表达式保证在 中被评估为 true ?

std::memset (
  reinterpret_cast<char*> (&a), // int a;
  (unsigned char)0,
  sizeof (int)
);

if (a == 0) {
  ...
}

通过阅读 C99 和 C++11 标准的引文(在本文后面),我们发现 C99 明确保证所有位都设置为 0< 的整数类型 将表示该类型中的值 0

我在 C++11 标准中找不到这种保证。

  • 没有这样的保证吗?
  • 上一个片段的结果真的是特定于实现的吗?


在 C99 (ISO/IEC 9899:1999) 中

5.2.1.2/1 Multibyte characters

A byte with all bits zero shall be interpreted as a null character independent of shift state. Such a byte shall not occur as part of any other multibyte character.

6.2.6.2/1 Integer types

The values of any padding bits are unspecified.45) A valid (non-trap) object representation of a signed integer type where the sign bit is zero is a valid object representation of the corresponding unsigned type, and shall represent the same value.

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



在 C++11 (ISO/IEC 14882:2011) 中

2.3/3     Character sets     [lex.charset]

The basic execution character set and the basic execution wide-character set shall each contain all the members of the basic source character set, plus control characters representing alert, backspace, and carriage return, plus a null character (respectively, null wide character), whose representation has all zero bits.

最佳答案

C++ 11

我认为相关的部分是

3.9.1/1 在 C++11 中

For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.

随着 3.9.1/7

The representations of integral types shall define values by use of a pure binary numeration system.

C11

6.2.6.2 很明确

For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1, so that objects of that type shall be capable of representing values from 0 to 2N − 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude);

— the sign bit has the value −(2M) (two’s complement);

— the sign bit has the value −(2M − 1) (ones’ complement).

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.

总结

我认为这两种标准的意图是相同的。

  • charsigned charunsigned char 具有参与值的所有位

  • 其他整数类型可能具有不参与值的填充位。它们中的错误位模式可能意味着无效值。

  • 解释是一种纯二进制表示,其定义在上面的 C11 引用中进行了扩展。

两点可能不清楚:

  • 可以 -0(对于符号和幅度和 _ones 的补码)是 C++ 中的陷阱值

  • 其中一个填充位可以是奇偶校验位(即,如果我们确保填充位不被修改,我们是否可以修改表示)

我会很保守,并认为两者都是。

关于c++ - 将int的所有字节都设置为(unsigned char)0,保证代表零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138188/

相关文章:

c++ - 由于变体成员,N3690/N4140 与 N4659/N4727,隐式定义的构造函数被删除

c++ - 错误处理语句是如何格式化的?

c# - 如何使用 size_t 遍历 uint64*,c# 的等价物是什么?

c++ - 全局变量不递增(gcc 疯了吗?)

c++ - 在对象外部循环遍历 unique_ptr 集合

c++ - std::string 的 operator< 是否应该受当前语言环境的影响?

c++ - 是否有可能让 NCurses CDK 矩阵动态调整到终端窗口?

c++ - 裁剪函数 BitBlt(...)

c++ - 将来调用 system_clock::now() 可以给出过去的时间吗?

无符号整数递增会导致未定义的行为吗?