无条件长到长 [英] cast unsigned long to long
问题描述
unsigned long a = ...;
long b =(long)a;
溢出时,b的结果是什么?
谢谢。
2005-12-27 ,jeff< xi ***** @ gmail.com>写道:
unsigned long a = ...;
long b =(long)a;
虽然溢出,结果是什么b?
谢谢。
undefined。
jeff说:
unsigned long a = ...;
long b =(long)a;
虽然溢出,b的结果是什么?
a不能溢出。如果a的值超过LONG_MAX,则b的值为
undefined。
-
Richard Heathfield >
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
" jeff" < XI ***** @ gmail.com>写道:unsigned long a = ...;
long b =(long)a;
虽然溢出,b的结果是什么? / blockquote>
演员阵容是不必要的;声明
long b = a;
是等价的,因为存在隐式转换。 (大多数演员都不需要
。)
如果转换为有符号整数类型溢出,结果
是实现定义的或实现定义的信号是
筹集的 (C99 6.3.1.3p3)。我认为在C99中获得信号的许可是新的b
;在C90中,你只需得到一个实现定义的结果。
(不,它不是未定义的。)
实现定义的意味着你的实现需要
记录它,但是你不应该依赖于它,因为它可能会因为一个实现而不同,所以你的代码
非便携式。
请注意,这与算术溢出时发生的情况不同,
调用未定义的行为签名类型。
-
Keith Thompson(The_Other_Keith) ks * **@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
解决方案On 2005-12-27, jeff <xi*****@gmail.com> wrote:
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
Thank you.
undefined.
jeff said:
unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
a can''t overflow. If the value of a exceeds LONG_MAX, the value of b is
undefined.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
"jeff" <xi*****@gmail.com> writes:unsigned long a = ...;
long b = (long)a;
while a overflows, what is the result of b?
The cast is unnecessary; the declaration
long b = a;
is equivalent, since there''s an implicit conversion. (Most casts are
unnecessary.)
If a conversion to a signed integer type overflows, "either the result
is implementation-defined or an implementation-defined signal is
raised" (C99 6.3.1.3p3). I think the permission to raise a signal is
new in C99; in C90, you just get an implementation-defined result.
(No, it''s not undefined.)
"Implementation-defined" means that your implementation is required to
document it, but you shouldn''t depend on this since it''s likely to
vary from one implementation to another, making your code
non-portable.
Note that this is different from what happens on artithmetic overflow,
which invokes undefined behavior for signed types.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于无条件长到长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!