热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么以下程序会发生溢出?-Whymayanoverflowoccurinthefollowingprogram?

voidmain(){inti;if(i<0){i-i;};}Cananyonehelpmetounderstandwhyanove
void main () {
  int i;
  if (i <0) { i = -i; };
}

Can anyone help me to understand why an overflow may occur in the above program?

任何人都可以帮我理解为什么在上述程序中可能出现溢出?

3 个解决方案

#1


13  

An overflow may occur because the range of integer representation in two's complement is not symmetric: the magnitude of the smallest negative number that can be represented is the magnitude of the highest positive number that can be represented, plus one. For example, on a 32-bit system the values are -2,147,483,648 and 2,147,483,647. That's why negating -2,147,483,648 would result in an overflow: the result of negation, a positive value 2,147,483,648, cannot be represented in an int of the same size.

可能会发生溢出,因为二进制补码中的整数表示范围不对称:可以表示的最小负数的大小是可以表示的最高正数的大小加一。例如,在32位系统上,值为-2,147,483,648和2,147,483,647。这就是为什么否定-2,147,483,648会导致溢出:否定的结果,正值2,147,483,648,不能用相同大小的int表示。

Note that the inverse of this problem is not true: negating a positive number would not result in an overflow:

请注意,此问题的反之亦然:否定正数不会导致溢出:

if (i > 0) { i = -i; } // No overflow here

#2


1  

Your value of "i", in the stack, is undefined when main starts. The start-up code that runs before main() is called can leave anything there.

堆栈中的“i”值在主启动时未定义。调用main()之前运行的启动代码可以在那里留下任何东西。

Addig to that what Kashif said, negative integers can go one value lower than non-negative integers as negatives don't need to leave room for zero. A "1" in the sign bit, with all remaining bits zero, causes an overflow when sign reversed.

加上Kashif所说的,负整数可以比非负整数低一个值,因为负数不需要留下零空间。符号位中的“1”,所有剩余的位为零,当符号反转时会导致溢出。

In 16 bits: -0x8000 == ~0x8000 + 1 == 0x7FFF + 1 == 0x8000
// "-" negative value == invert + 1 == inverted + 1 == final is the same

在16位:-0x8000 == ~0x8000 + 1 == 0x7FFF + 1 == 0x8000 //“ - ”负值==反转+ 1 ==倒置+ 1 ==最终是相同的

The likely hood of this value is low, but present. It will not happen unless the stack just happens to contain the offending number.

这个值的可能性很低,但存在。除非堆栈恰好包含有问题的数字,否则不会发生。

#3


0  

The cause for an integer overflow is when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of bits, either larger than the maximum or lower than the minimum representable value.

整数溢出的原因是算术运算尝试创建一个数值,该数值超出了可以用给定位数表示的范围,该位数大于最大值或小于最小可表示值。

  • Well, in your case the variable i is not initialized. So what will happen here is that the memory space which is assigned to variable i of integer type will be containing some garbage value.
  • 那么,在你的情况下,变量i没有初始化。那么这里会发生的是,分配给整数类型的变量i的内存空间将包含一些垃圾值。

  • If by any chance that memory address contains the maximum possible integer value which is -2^31(-2,147,483,648) then negating this value will result in integer overflow.
  • 如果内存地址有可能包含最大可能的整数值-2 ^ 31(-2,147,483,648),则否定该值将导致整数溢出。

I hope this helps.

我希望这有帮助。


推荐阅读
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文整理了Java中org.gwtbootstrap3.client.ui.Icon.addDomHandler()方法的一些代码示例,展示了Icon.ad ... [详细]
  • location,locationManager
    https:stackoverflow.comsearch?tabvotes&q[android]locationisnullhttps:developer.android.go ... [详细]
  • 本文整理了Java中org.apache.pig.backend.executionengine.ExecException.<init>()方法的一些代码 ... [详细]
  • vb.net面试题,请大家帮忙,谢谢。如果需要讲详细一点,那就加我QQ531412815第4题,潜在的错误,这里的错误不是常规错误,属于那种只有在运行是才知道的错误:Catchex ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了css回到顶部按钮相关的知识,希望对你有一定的参考价值。 ... [详细]
  • AndroidStudio 2.3迁移3.0踩坑之——Could not resolve project
    参见StackOverflow如果你的项目引用了自己的库,在迁移到3.0后,编译就会报错。Error:Failedtoresolve:Couldnotresolveproject: ... [详细]
author-avatar
key920721
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有