unsigned int中的负值 [英] Negative values in unsigned int

查看:97
本文介绍了unsigned int中的负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在IOS 9899-99的草稿

版本中找到以下问题的答案。当你将一个

unsigned int减少到0以下时(或者减去两个带有负数的无符号整数/ $
结果),语义是什么? AFAIR,这导致实现定义的行为,但

不是未定义的行为。这是正确的吗?


那么标准对这段代码说了什么?


void f(无效)

{

unsigned int n = 0;

n - ;

}


另外,我是否有实现定义或未定义的行为,如果我将
递减到零以下但不使用该值,如下面的

代码?


void f(unsigned int n)

{

while(n-- 0){/ *在最后一次循环迭代中,n是

;从0递减但未使用

}之后* /

}


标准中带有节号的简短通知会很好

(因为我觉得我的选秀版中的章节编号不是那个

不同)。


urs

解决方案

2008年9月8日星期一14:38:16 +0200,Urs Thuermann写道:


那么标准对这段代码说了什么?

unsigned int n = 0;

n--;



总是产生UINT_MAX。


此外,我是否有实现定义或未定义的行为,如果我将
递减到零以下但不使用该值,如下面的代码所示?


void f(unsigned int n)

{

while(n-- 0){/ *在最后一次循环迭代中,n是

;从0递减但未使用

}之后* /

}



这会评估大于原始值为n,则

递减n。 n是自动的,因此当函数

返回时它的值会丢失。


Urs Thuermann写道:


我无法在IOS 9899-99的草案

版本中找到以下问题的答案。当你将一个

unsigned int减少到0以下时(或者减去两个带有负数的无符号整数/ $
结果),语义是什么? AFAIR,这导致实现定义的行为,但

不是未定义的行为。那是对的吗?



否。这是标准定义的行为。第6.2.5p9节说:


"涉及无符号操作数的计算永远不会溢出,因为

a结果无法用结果无符号整数表示

类型以模数的形式减少,该数字大于可由结果类型表示的最大值

值。


那么标准对这段代码说了什么?


void f(无效)

{

unsigned int n = 0;

n--;



此时,n的值必须是UINT_MAX。


}


另外,我是否有实现定义或未定义的行为,如果我将
递减到零以下但不使用该值,如下所示

代码?


void f(unsigned int n)

{

while(n-- 0){/ *在最后一次循环迭代中,n是

;从0递减但未使用

}之后* /

}



由于行为是标准的 - 定义,你不必担心

;是否使用结果值无关紧要。如果

行为未定义(例如,INT_MAX + 1),那么你没有使用结果的

事实仍然无法保护你的代码,至少在原则上是
。实际上,由于使用的值不是
,优化编译器可能会删除

产生该值的计算,从而减少了不合需要的机会

后果 - 但你永远不应该编写依赖于此的代码。


Urs Thuermann写道:


>

我无法在IOS 9899-99的

草案版本中找到以下问题的答案。当你将
减少一个低于0的无符号整数(或减去两个无符号整数

且结果为负)时,语义是什么? AFAIR,这导致实现 -

定义的行为,但不是未定义的行为。那是对的吗?



编号经过仔细定义。无符号数量算术算术

模数(最大无符号值+ 1)。


-

[邮件]:Chuck F (cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。


I wasn''t able to find an answer to the following questions in a draft
version of IOS 9899-99. What is the semantic when you decrement an
unsigned int below 0 (or subtract two unsigned ints with negative
result)? AFAIR, this results in implementation-defined behavior but
not undefined behavior. Is that correct?

So what does the standard say to this code?

void f(void)
{
unsigned int n = 0;
n--;
}

Also, do I have implementation-defined or undefined behavior, if I
decrement below zero but don''t use the value, as in the following
code?

void f(unsigned int n)
{
while (n-- 0) { /* In the last loop iteration, n is
; decremented from 0, but not used
} afterwards */
}

A short notice with section number in the standard would be nice
(as I think section numbering in my draft version is not that
different).

urs

解决方案

On Mon, 08 Sep 2008 14:38:16 +0200, Urs Thuermann wrote:

So what does the standard say to this code?
unsigned int n = 0;
n--;

Always results in UINT_MAX.

Also, do I have implementation-defined or undefined behavior, if I
decrement below zero but don''t use the value, as in the following code?

void f(unsigned int n)
{
while (n-- 0) { /* In the last loop iteration, n is
; decremented from 0, but not used
} afterwards */
}

This evaluates the greater-than with the original value of n, then
decrements n. n is automatic so its value is lost when the function
returns.


Urs Thuermann wrote:

I wasn''t able to find an answer to the following questions in a draft
version of IOS 9899-99. What is the semantic when you decrement an
unsigned int below 0 (or subtract two unsigned ints with negative
result)? AFAIR, this results in implementation-defined behavior but
not undefined behavior. Is that correct?

No. It is standard-defined behavior. Section 6.2.5p9 says:

"A computation involving unsigned operands can never overflow, because
a result that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the largest
value that can be represented by the resulting type."

So what does the standard say to this code?

void f(void)
{
unsigned int n = 0;
n--;

At this point, the value of n must be UINT_MAX.

}

Also, do I have implementation-defined or undefined behavior, if I
decrement below zero but don''t use the value, as in the following
code?

void f(unsigned int n)
{
while (n-- 0) { /* In the last loop iteration, n is
; decremented from 0, but not used
} afterwards */
}

Since the behavior is standard-defined, you don''t have to worry about
it; whether or not the resulting value gets used doesn''t matter. If
the behavior were undefined (as, for example, INT_MAX+1), then the
fact that you didn''t use the result still wouldn''t protect your code,
at least in principle. As a practical matter, since the value is not
used, an optimizing compiler might remove the calculation that
produced the value, reducing the opportunities for undesireable
consequences - but you should never write code which relies upon this.


Urs Thuermann wrote:

>
I wasn''t able to find an answer to the following questions in a
draft version of IOS 9899-99. What is the semantic when you
decrement an unsigned int below 0 (or subtract two unsigned ints
with negative result)? AFAIR, this results in implementation-
defined behavior but not undefined behavior. Is that correct?

No. It is carefully defined. Unsigned quantities do arithmetic
modulo the (maximum unsigned value + 1).

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


这篇关于unsigned int中的负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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