从int16_t到int的对话能否导致实现定义的行为? [英] Can a int16_t to int conversation result in implementation-defined behavior?

查看:186
本文介绍了从int16_t到int的对话能否导致实现定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99标准的第7.18.1.1节第1段中:

In section 7.18.1.1 paragraph 1 of the C99 standard:

typedef名称 intN_t 指定宽度为 N 且没有填充的带符号整数类型 位和两个补码表示形式.

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation.

根据C99标准,要求精确宽度的带符号整数类型具有二进制补码表示形式.例如,这意味着int8_t的最小值为-128,而不是一个人的补语 的最小值为-127.

According to the C99 standard, exact-width signed integer types are required to have a two's complement representation. This means, for example, int8_t has a minimum value of -128 as opposed to the one's complement minimum value of -127.

第6.2.6.2节第2段允许实现决定将符号位解释为符号和大小二进制补码还是一个人的补语:

Section 6.2.6.2 paragraph 2 allows the implementation to decide whether to interpret a sign bit as sign and magnitude, two's complement, or one's complement:

如果符号位为1,则值应为 已通过以下方式之一进行了修改:
—取反了符号位0的对应值(符号和幅度);
—符号位的值为-(2 N )(二进制补码);
—符号位的值为-(2 N -1)(一个人的补语).

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 -(2N) (two’s complement);
— the sign bit has the value -(2N - 1) (ones’ complement).

方法之间的区别很重要,因为二进制补码(-128)中整数的最小值可能超出 one补码表示的值范围>(-127127).

The distinct between the methods is important because the minimum value of an integer in two's complement (-128) can be outside the range of values representable in ones' complement (-127 to 127).

假设一个实现将int类型定义为具有ones' complement表示形式,而int16_t类型具有two's complement表示形式(由C99标准保证).

Suppose an implementation defines the int types as having ones' complement representation, while the int16_t type has two's complement representation as guaranteed by the C99 standard.

int16_t foo = -32768;
int bar = foo;

在这种情况下,由于foo持有的值超出了bar可以表示的值范围,从int16_tint的转换是否会引起实现定义的行为?

In this case, would the conversion from int16_t to int cause implementation-defined behavior since the value held by foo is outside the range of values representable by bar?

推荐答案

是.

具体来说,该转换将产生实现定义的结果. (对于-32768以外的任何值,其结果和行为都将得到很好的定义.)或者该转换可能会产生一个实现定义的信号,但是我不知道执行该操作的实现.

Specifically, the conversion would yield an implementation-defined result. (For any value other than -32768, the result and the behavior would be well defined.) Or the conversion could raise an implementation-defined signal, but I don't know of any implementations that do that.

有关转换规则的参考: N1570 6.3.1.3p3 :

Reference for the conversion rules: N1570 6.3.1.3p3:

否则,将对新类型进行签名,并且无法表示该值 在里面;结果要么是实现定义的,要么是 实施定义的信号被引发.

Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

这只能在以下情况下发生:

This can only happen if:

  • int是16位(更确切地说,具有15个值位,1个符号位和0个或更多填充位)
  • int使用补码或正负号和幅度
  • 该实现还支持二进制补码(否则将不会定义int16_t).
  • int is 16-bits (more precisely, has 15 value bits, 1 sign bit, and 0 or more padding bits)
  • int uses one's-complement or sign-and magnitude
  • The implementation also supports two's-complement (otherwise it just won't define int16_t).

我很惊讶地看到一个符合这些标准的实现.它必须同时支持二进制补码和一个或两个数的补码,并且必须为类型int选择后者中的一个. (也许非二进制补码实现可能在软件中支持二进制补码,只是为了能够定义int16_t.)

I'd be surprised to see an implementation that meets these criteria. It would have to support both two's-complement and either one's complement or sign-and-magnitude, and it would have to chose one of the latter for type int. (Perhaps a non-two's-complement implementation might support two's-complement in software, just for the sake of being able to define int16_t.)

如果您担心这种可能性,则可以考虑将其添加到您的头文件之一:

If you're concerned about this possibility, you might consider adding this to one of your header files:

#include <limits.h>
#include <stdint.h>

#if !defined(INT16_MIN)
#error "int16_t is not defined"
#elif INT_MIN > INT16_MIN
#error "Sorry, I just can't cope with this weird implementation"
#endif

#error不太可能在任何理智的现实世界实现中触发.

The #errors are not likely to trigger on any sane real-world implementation.

这篇关于从int16_t到int的对话能否导致实现定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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