RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1280966
Accepted
AlexGlebe
AlexGlebe
Asked:2022-05-12 16:32:57 +0000 UTC2022-05-12 16:32:57 +0000 UTC 2022-05-12 16:32:57 +0000 UTC

越界检查

  • 772

Nastasia想问:https ://ru.stackoverflow.com/questions/1280905/Checking-for-out-of-int/1280957#1280957

我正在通过std::atoi()将string转换为int,但是string可以超出int值的范围(内部string可以大于2 147 483 647)

我想通过字符数来检查,如果超过 7 个,以便给出错误,然后逐个字符地检查字符串的最大数量。如果我执行算术运算,就会出现问题,出现越界。如何正确检查数字是否超出其值范围?

我试着这样回答:

有两因素身份验证之类的东西。在我们的例子中,从字符串中读取数字,转换为新字符串,然后进行比较。

# include <string>
# include <sstream>
# include <iostream>
bool isBADstrtoint(std::string const & st,int & in) {
    int i = std::atoi(st.c_str());
    std::stringstream ss;
    ss << i;
std:: cout<<"ss.str = '"<<ss.str()<<"'"<<std::endl;
    if (st == ss.str()) {
        in = i ;
        return false ; }
    return true ; }

int main(){
    int i;
    std::string st = "12345678901";
    if( isBADstrtoint(st,i))
      std::cout<<"st = '"<<st <<"' is bad"<<std::endl;  
    else
      std::cout<<"i = "<<i <<std::endl;
}

仅适用于整数,不接受指数。

ss.str = '-539222987'
st = '12345678901' is bad

使用算术运算时,您应该已经立即自己检查溢出。

int x, y , s ;
s = x + y ;
if ( y > 0 )
  if ( s > x )
    Ok
  else 
    Bad
else
  if ( y < 0 )
    if ( s < x )
      Ok
    else
      Bad

但我的回答被否决了。他们说,对于不能表示这种类型数字的字符串,编译器可以返回任何数字,进一步执行并不能保证使用这个返回值的程序代码的进度。

加数也是一样。如果发生溢出,编译器在比较相等==或<.

这是一个真实的例子,当 UB 发生时,int 类型的行为就好像它可以表示的值的范围比它实际的范围要大。–沃洛洛

#include <iostream>
#include <limits>
#include <type_traits>
using std::cout;
using std::endl;

int main()
{
  volatile int tmp = std::numeric_limits<int>::max();
  
  int a = tmp;
  
  cout << "a+1 > a: " << ( int(a+1) > a ) << endl;
  
  cout << "a+1:     " << int(a+1)      << endl;
  cout << "a:       " << a             << endl;
}

g++ -std=c++2a -O3 -Wall -pedantic -pthread main.cpp && ./a.out
a+1 > a: 1
a+1:     -2147483648
a:       2147483647

如何检查字符串中的数字是否可以通过int类型正确提供?

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    user7860670
    2022-05-12T17:13:56Z2022-05-12T17:13:56Z

    这是一个使用检查库实现的示例:

    1. 使用 boost.lexical cast 转换字符串:
    #include <boost/lexical_cast.hpp>
    
    #include <iostream>
    
    int main()
    {
        try
        {
            auto const value{::boost::lexical_cast<int>("33423049234")};
            ::std::cout << "good " << value;
        }
        catch (::boost::bad_lexical_cast const & exception)
        {
            ::std::cout << "bad";
        }
        return 0;
    }
    

    https://godbolt.org/z/jqP7szqYf

    1. 使用 boost.safe 数字来控制算术运算:
    #include <boost/safe_numerics/safe_integer.hpp>
    
    #include <iostream>
    #include <limits>
    #include <system_error>
    
    int main()
    {
        ::boost::safe_numerics::safe<int> const x{::std::numeric_limits<int>::max()};
        ::boost::safe_numerics::safe<int> const y{1};
        try
        {
            auto const result{x + y};
            ::std::cout << "good " << static_cast<int>(result);
        }
        catch (::std::system_error const & exception)
        {
            ::std::cout << "bad " << exception.code();
        }
        return 0;
    }
    

    https://godbolt.org/z/Wsb5Gsjvz

    • 3
  2. Pak Uula
    2022-05-12T17:18:58Z2022-05-12T17:18:58Z

    答案在很大程度上取决于“Nastasya”的含义,即“正确检查一个数字是否超出其值”。

    例如,使用编译器扩展是否“正确”?如果是,那么GCC和Clang具有用于溢出的整数运算的原语。

    使用特殊平台 API是否“正确” ?例如 Windows intsafe.h?

    这个概念是否“正确”使用了第三方库?例如,安全数学.h

    是否可以在汇编程序上“正确”编写?为检查溢出标志的目标平台自己实现加法/减法/乘法函数。

    如果“正确”是指严格遵守标准,那么在标准中溢出是未定义的行为。换句话说,需要对结果和操作数进行额外检查:

    1. 添加正数的结果大于任何项
    2. 负数相加的结果小于任何加数
    3. 乘正整数时,确定每个因子中最高有效位的位置,并确保这些位置的总和严格小于类型的位大小。

    像这样的东西。

    更新

    正如@gbg 正确指出的那样,发生溢出后,检查为时已晚,因为未定义行为的选项可能不同。例如,打开自毁系统,如阿丽亚娜 5 号火箭

    因此,您需要提前检查。例如,当添加если a > 0 то b <= (MAX_INT - a)

    • 3

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5