由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 经典题atoi的溢出处理 (转载)
相关主题
一个integer promotion问题看了那个招聘的帖子,自觉需要把 atoi温习一下。赶紧的
[合集] 【求助】如何将c string中的string转换成int?谢谢如何把一个char转换成一个int啊?
请教C的类型转换问题奇怪的问题:关于一个简单的malloc()小程序 (转载)
有人发过的一个面试题C, how is a string cast into a int?
gcc 优化不优化运算结果不一样?gcc 的 bug?[合集] 编译报错stack overflow,到底来自何处
这段代码为何要这样做?借人气问个c++的overflow (转载)
pointer overflow我也来个。某公司招初级C程序员的面试题。[转载]
a question about bitwise operationunsigned int subtract int
相关话题的讨论汇总
话题: int话题: val话题: atoi话题: str话题: max
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: gandjmitbbs (Nothing), 信区: JobHunting
标 题: 经典题atoi的溢出处理
发信站: BBS 未名空间站 (Tue Feb 22 21:40:03 2011, 美东)
int atoi(const char* s);
assuming INT_MAX = 2^31-1, INT_MIN = -2^31, the following must hold:
atoi("2147483648") = 2147483647
atoi("-2147483649") = -2147483648
but this seems a little tricky. any elegant solution to handle the
overflow?
g*********s
发帖数: 1782
2
this is my solution casting an unsigned int to an int. but gcc gives me
warning:
string_api.cpp: In function ‘bool overflow(unsigned int, bool, char)’:
string_api.cpp:330: warning: integer overflow in expression
string_api.cpp: In function ‘int str2int(const char*)’:
string_api.cpp:346: warning: integer overflow in expression
explict type casting can fix the warning. but i don't like this solution
very much. anyone has a better alternative?
static
bool overflow(unsigned int val, bool negative, char c) {
return val > (negative ? INT_MAX + 1 : INT_MAX); // line 330
}
int str2int(const char* str) {
if ( str == NULL || str[0] != '+' && str[0] != '-' &&
!isdigit(str[0]) ) {
return 0;
}
int idx = 0;
bool negative = (str[0] == '-');
if ( negative ) {
idx ++;
}
unsigned int val = 0;
while ( isdigit(str[idx]) ) {
unsigned int next_val = 10 * val + char2int(str[idx]);
if ( overflow(next_val, negative, str[idx]) ) {
val = negative ? INT_MAX + 1 : INT_MAX; // line
346
break;
}
else {
val = next_val;
}
idx ++;
}
return (negative ? -val : val);
}

【在 g*********s 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: gandjmitbbs (Nothing), 信区: JobHunting
: 标 题: 经典题atoi的溢出处理
: 发信站: BBS 未名空间站 (Tue Feb 22 21:40:03 2011, 美东)
: int atoi(const char* s);
: assuming INT_MAX = 2^31-1, INT_MIN = -2^31, the following must hold:
: atoi("2147483648") = 2147483647
: atoi("-2147483649") = -2147483648
: but this seems a little tricky. any elegant solution to handle the
: overflow?

t****t
发帖数: 6806
3
mixing unsigned and int in one expression, and rely on implicit type
conversion is dangerous. better do explicit type conversion.
remember: numeric literal has type as well. 1, 1U, 1L, 1UL, 1.0, 1.0F are
all different types.

【在 g*********s 的大作中提到】
: this is my solution casting an unsigned int to an int. but gcc gives me
: warning:
: string_api.cpp: In function ‘bool overflow(unsigned int, bool, char)’:
: string_api.cpp:330: warning: integer overflow in expression
: string_api.cpp: In function ‘int str2int(const char*)’:
: string_api.cpp:346: warning: integer overflow in expression
: explict type casting can fix the warning. but i don't like this solution
: very much. anyone has a better alternative?
: static
: bool overflow(unsigned int val, bool negative, char c) {

c**b
发帖数: 2999
4
把每个string分成2半转换,然后合起来?
比如 2147483648, 分成2个string 214748 和3648,分别转换成int,然后合起来.

【在 g*********s 的大作中提到】
: this is my solution casting an unsigned int to an int. but gcc gives me
: warning:
: string_api.cpp: In function ‘bool overflow(unsigned int, bool, char)’:
: string_api.cpp:330: warning: integer overflow in expression
: string_api.cpp: In function ‘int str2int(const char*)’:
: string_api.cpp:346: warning: integer overflow in expression
: explict type casting can fix the warning. but i don't like this solution
: very much. anyone has a better alternative?
: static
: bool overflow(unsigned int val, bool negative, char c) {

g*********s
发帖数: 1782
5
thx for the reminder. yes, it's not a good idea to mixed up unsigned int
and int.

are

【在 t****t 的大作中提到】
: mixing unsigned and int in one expression, and rely on implicit type
: conversion is dangerous. better do explicit type conversion.
: remember: numeric literal has type as well. 1, 1U, 1L, 1UL, 1.0, 1.0F are
: all different types.

1 (共1页)
进入Programming版参与讨论
相关主题
unsigned int subtract intgcc 优化不优化运算结果不一样?gcc 的 bug?
C++中parse string的问题这段代码为何要这样做?
new了指针,delete的时候出错了pointer overflow
[转载] Re: 问个土问题吧a question about bitwise operation
一个integer promotion问题看了那个招聘的帖子,自觉需要把 atoi温习一下。赶紧的
[合集] 【求助】如何将c string中的string转换成int?谢谢如何把一个char转换成一个int啊?
请教C的类型转换问题奇怪的问题:关于一个简单的malloc()小程序 (转载)
有人发过的一个面试题C, how is a string cast into a int?
相关话题的讨论汇总
话题: int话题: val话题: atoi话题: str话题: max