整数转换(变窄,加宽),未定义的行为 [英] Integer conversions(narrowing, widening), undefined behaviour

查看:213
本文介绍了整数转换(变窄,加宽),未定义的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是pretty我很难找到有关此主题的方式,我可以很容易地理解信息,所以我要求什么,我有一个检讨found.It的所有有关转换,只转换。


在我的例子将参照:

 (符号/无符号)诠释较大;
(符号/无符号)中,CHAR小;


  1. 截断整数。 (超大化>小)


    • 第一个 MSB 侧,以匹配尺寸。结果

    • 第二,转换截断结果的符号/无符号取决于较小的类型。

    结果
    如果更大的值过大,以适应较小的类型,它会导致不确定的行为(指正在这一点)。但是我的规则应该是在所有机器上工作(指正上,太)和结果应该是predictable。


  2. 拓宽整数(smaller->大)

    A)符号字符 - > 符号int


    • prePEND与MSB(1或0),以适应更大的尺寸

    • 转换为签订

    B)符号字符 - > unsigned int类型


    • prePEND与MSB(1或0),以适应更大的尺寸。

    • 转换为无符号的搜索结果

    C) unsigned char型 - > 符号int


      用0
    • prePEND匹配更大尺寸

    • 转换为签订

    D) unsigned char型 - > unsigned int类型


      用0
    • prePEND匹配更大尺寸

    • 转换为无符号


在哪里是我没有'提未定义/未指定的行为,会弹出?


解决方案

这是不可或缺的转换从未产生不确定的行为(可生产实现定义的行为)。

一个转换为一个类型,可以重新present值被转换始终是明确的:价值只是保持不变。

A转换为无符号类型始终是明确的:值取模UINT_MAX + 1(或任何最大值目标类型坦承)

一个转换为符号的类型不能再present价值在任何一个实现定义的值,或实现定义的信号被转换结果。

请注意,上述规则在整数值来定义,而不是在比特序列的形式来

It was pretty difficult for me to find information on this subject in manner that I could easily understand, so I'm asking for a review of what I have found.It's all about conversion and conversion only.


In examples I will be referring to:

(signed/unsigned) int bigger;
(signed/unsigned) char smaller;

  1. Truncating integers. (bigger->smaller)

    • first truncate bigger on MSB side to match smaller size.
    • second, convert truncated result to signed/unsigned depending on smaller type.


    If bigger value is too big to fit in smaller type, it results in undefined behaviour (correct me on that). However my rule should be working on all machines (correct me on that, too) and results should be predictable.

  2. Widening integers (smaller->bigger)

    a) signed char -> signed int

    • prepend smaller with MSB (1 or 0) to match bigger size
    • convert to signed

    b) signed char -> unsigned int

    • prepend smaller with MSB (1 or 0) to match bigger size.
    • convert to unsigned

    c) unsigned char -> signed int

    • prepend with 0's to match bigger size
    • convert to signed

    d) unsigned char -> unsigned int

    • prepend with 0's to match bigger size
    • convert to unsigned

Where are undefined/unspecified behaviours that I didn' mention that could pop up?

解决方案

An integral conversion never produces undefined behaviour (it can produce implementation-defined behaviour).

A conversion to a type that can represent the value being converted is always well-defined: the value simply stays unchanged.

A conversion to an unsigned type is always well-defined: the value is taken modulo UINT_MAX+1 (or whatever maximum value the target type admits).

A conversion to a signed type that cannot represent the value being converted results in either an implementation-defined value, or an implementation-defined signal.

Note that the above rules are defined in terms of integer values and not in terms of sequences of bits.

这篇关于整数转换(变窄,加宽),未定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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