Set all bytes of int to (unsigned char)0, guaranteed to represent zero?

前端 未结 4 671
悲哀的现实
悲哀的现实 2020-11-30 08:10

This is not a matter of recommended practise (nor undefined behavior), but about what the c++-standard actually guarantees in th

4条回答
  •  被撕碎了的回忆
    2020-11-30 09:06

    No. I don't believe it's actually guaranteed, but it's rather vague.

    I'd be very surprised if there has ever been a C++ implementation in which all-bits-zero is not a representation of 0, but I believe such an implementation could be conforming (though perverse).

    Let's start by considering the C99 standard. (Yes, I know, the question is about C++; bear with me.) It says that the bits of the object representation of an unsigned integer type are divided into two groups: value bits and padding bits (there needn't be any padding bits, and most implementations don't have them). The value bits make up a pure binary representation; the padding bits do not contribute to the value. Some combinations of padding bits might generate a trap representation.

    Signed types are similar, with the addition of a single sign bit. Signed types can be represented using either sign and magnitude, or two's complement, or one's complement -- but again, any padding bits do not contribute to the value, and some combinations of padding bits can generate trap representations.

    This description does not exclude the possibility that, for example, an integer type wider than char might have a single padding bit that must always be 1; if it's 0, you have a trap representation. Or, perhaps more plausibly, it might have an odd parity bit.

    After the C99 standard was published, the second Technical Corrigendum added the following sentence, which also appears in C11.

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

    I'll emphasize that this was added as normative text, not as a footnote, which suggests (but doesn't prove) that the committee members felt that the guarantee wasn't already implicit in the C99 standard.

    (C90 was far less specific about how integer types are represented. It didn't mention padding bits, trap representations or two's-complement et al. I would argue that it gave implementations at least as much flexibility as C99.)

    So starting with C99 TC2, the C language guarantees that all-bits-zero is a representation of zero for any integer type. In C99 and C90, that guarantee is not stated.

    That's C. What about C++?

    The 2011 C++ standard seems to provide only slightly more specificity about integer type representations as the old 1990 C standard. It does require signed types to be represented using either 2's complement, 1's complement, or signed magnitude. It also requires a "pure binary numeration system". It doesn't mention "trap representations", nor does it discuss padding bits except in the context of bit fields.

    So, in both C90 and pre-TC2 C99 it was at least theoretically possible for all-bits-zero to be a trap representation for an integer type. The C++ standard's requirements for integer types are very similar to those of C90 and C99. It does require a "pure binary representation", but I would argue that that applies, as it does in C99, only to the value bits; though C++ doesn't mention padding bits, it doesn't forbid them.

    Again, this is mainly of theoretical interest (thus the "language-lawyer" tag). The C committee felt free to impose the requirement that all-bits-zero must be a representation of zero because all implementations already satisfied it. The same almost certainly applies to C++.

提交回复
热议问题