一个普通的`char`可能有陷阱值吗? [英] Can a plain `char` possibly have trap values?

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

问题描述

自述文件

类型为T陷阱值" 陷阱表示形式" 是(基础存储的)位组合,产生的无效值T.试图解释无效值的表示形式将导致未定义的行为.

README

A "trap value", or "trap representation" for type T, is a bit combination (of the underlying storage) that yields an invalid value of T. Trying to interpret the representation of an invalid value will cause undefined behavior.


让战斗开始吧.

另一个问题开始了有关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?

3.9.1p1 基本类型 [basic.fundamental]

char是否可以包含负值,这是实现定义的.可以将字符明确声明为signedunsigned.

It is implementation-defined whether a char can hold negative values. Characters can be explicitly declared signed or unsigned.

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.

3.9p2 类型 [basic.types]

对于琐碎可复制类型T,的任何对象(基类子对象除外),无论该对象是否具有类型T的有效值,都可以复制组成该对象的基础字节(1.7)放入charunsigned char.

For any object (other than a base-class subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes (1.7) making up the object can be copied into an array of char or unsigned char.

如果将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`可能有陷阱值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆