Can a plain `char` possibly have trap values?

后端 未结 1 384
广开言路
广开言路 2021-01-01 11:52

README

A "trap value", or "trap representation" for type T, is a bit combination

相关标签:
1条回答
  • 2021-01-01 12:14

    The standard tells us there must be:

    • char, signed char, unsigned char, all the same size
    • the sizeof(char) is 1
    • char has at least 8 bits
    • every bit combination is meaningful and valid
    • array of char is packed (or behaves is if it is).

    There isn't much wiggle room.

    Nevertheless there are suggestions that during certain kinds of operations such as loading uninitialised memory or conversions as trap might occur.

    Yes, I think an implementation could have a trap representation where trap values could occur as a result of some kind of undefined or unspecified behaviour, including evaluating expressions that involve unspecified/uninitialised values. The actual bit pattern leading to a trap value would be invisible to the implementation.

    Such a CPU could have 9 bit bytes where only 8 bits are visible to the compiler and runtime, and the 9th bit is used to detect uninitialised memory, and will trigger a trap if loaded by (unprivileged) instructions.

    0 讨论(0)
提交回复
热议问题