C ++下溢和上溢 [英] C++ underflow and overflow

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

问题描述

我是新来的人,因此,如果有任何问题,请告诉我,下次我会尽力而为。

Hi I am new in here so please let me know if anything is wrong and I will try to better the next time .

我试图了解C ++中下溢和上溢的工作方式。我的理解是,如果超出变量范围,它将从范围的另一端开始。因此,如果short的最小值为-32768,并且对它执行-1,则新值应为SHRT_MAX。(32767)
这是我的代码:

I am trying to understand how underflow and overflow works in C++ .My understanding is if a variable's range is exceeded it will start from the other end of the range . Thus if minimum of short is -32768 and if we do a -1 to it the new value should be SHRT_MAX .(32767) Here is my code:

#include<iostream.h>
#include<limits.h>
#include<conio.h>
int main ( void )
{
 int testpositive =INT_MIN ;
 short testnegative = SHRT_MIN ; 
 cout<< SHRT_MIN<<"\n";
 cout << testnegative-1<<"\n";
 cout << INT_MIN << "\n";
 cout << testpositive-1 << "\n"; 
 cout<<testpositive-2;
 getch();
 return 0;
}   


推荐答案

溢出时的确切行为/仅为 unsigned 类型指定下溢。对于普通的带符号整数类型,C ++标准只是简单地说什么都不会发生。

The exact behavior on overflow/underflow is only specified for unsigned types. For normal signed integer types instead the C++ standard simply says than anything can happen.

如果我们谈论的是x86处理器(或大多数其他现代处理器),则其行为确实是完全符合您的描述,对于CPU,有符号值或无符号值之间没有区别(有符号和无符号运算,但值本身只是位)。

If we're talking about x86 processor (or most other modern processors) indeed the behavior is exactly what you describe and for the CPU there is no difference between a signed value or an unsigned value (there are signed and unsigned operations, but the value themselves are just bits).

请注意,编译器可以假设(并且大多数现代优化编译器实际上都假设)在正确的程序中,例如在诸如以下代码中,不会发生有符号整数溢出:

Note that compilers can assume (and most modern optimizing compilers actually DO assume) that no signed integer overflow can occur in a correct program and for example in code like:

int do_something();
int do_something_else();

void foo() {
    int x = do_something();
    int y = x + 1;
    if (x < y) {
        do_something();
    } else {
        do_something_else();
    }
}

编译器可以自由跳过测试,而 else 分支完全在生成的代码中,因为在有效程序中,带符号的int x 总是小于 x + 1 (因为签名溢出不能视为有效行为)。
如果将 int 替换为 unsigned int ,则编译器必须为测试和其他代码生成代码分支,因为对于无符号类型, x> x + 1

a compiler is free to skip the test and the else branch in the generated code completely because in a valid program a signed int x is always less than x+1 (as signed overflow cannot be considered valid behavior). If you replace int with unsigned int however the compiler must generate code for the test and for the else branch because for unsigned types it's possible that x > x+1.

例如clang将 foo 的代码编译为

For example clang compiles the code for foo to

foo():                                # @foo()
        push    rax
        call    do_something()
        pop     rax
        jmp     do_something()       # TAILCALL

在这里您可以看到ode只调用 do_something 两次(对 rax 的奇怪处理除外),并且没有提及 do_something_else 实际上存在。 gcc 或多或少会生成相同的代码。

where you can see that the ode just calls do_something twice (except for the strange handling of rax) and no mention of do_something_else is actually present. More or less the same code is generated by gcc.

这篇关于C ++下溢和上溢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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