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.
|
|