本文介绍了一个普通的`char`可能有陷阱值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


让战斗开始吧.

另一个问题开始了有关char的激烈讨论,以及实施具有.


Let the battle begin..

Another question has started a heated discussion regarding char, and the possibility of an implementation having trap representations for it.

问题:

  • char可能有陷阱值吗?
  • Can char possibly have trap values?

这些部分是上一论点中引用次数最多的部分,它们有矛盾之处吗?

These sections are the most quoted ones during the previous argumentation, are they contradicting?

A char,signed char,unsigned char占用相同的存储量,并且具有相同的对齐要求(3.11);也就是说,它们具有相同的对象表示形式.对于字符类型,对象表示形式的所有位都参与值表示形式.

A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation. 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.

在任何特定的实现中,普通的char对象都可以采用与signed charunsigned char;相同的值,而signed charunsigned char;是实现定义的.

In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

如果将charunsigned char数组的内容复制回该对象,则该对象随后应保留其原始值.

If the content of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original value.

推荐答案

该标准告诉我们必须存在:

The standard tells us there must be:

  • 字符,已签名字符,未签名字符,大小均相同
  • (char)的大小是1
  • char至少有8位
  • 每一位组合都是有意义且有效的
  • char数组被打包(或者如果是,则表现为).

没有太多的摆动空间.

尽管如此,有人建议在某些类型的操作期间,例如加载未初始化的内存或进行陷阱转换.

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.

这样的CPU可能有9位字节,其中编译器和运行时仅可见8位,而第9位用于检测未初始化的内存,如果被(非特权)指令加载,则会触发陷阱.

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.

这篇关于一个普通的`char`可能有陷阱值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:22