Solved

Checking For An Integer Out Of Range

Posted on 1998-09-29
20
242 Views
Last Modified: 2010-04-15
I am in a project where the user enters a number into a string.  Before I convert this string to an integer I would like to check to see if it exceeds the defined maximum integer limit set in limits.h.

If you try and convert an integer out of range it will overflow.

I have tried several methods and have no been able to find a successful solution.

Any suggestions
Darrell
0
Comment
Question by:larockd
  • 8
  • 8
  • 2
  • +2
20 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 10 total points
Comment Utility
1. double usually has a wider range than int

2. you want to make sure any comparisons are done using double against double

3. Example

double d = atof( string ) ;
double maxint = MAX_INT ;

if ( d > maxint ) { /* Too large */ }


0
 
LVL 8

Expert Comment

by:jhurst
Comment Utility
I would suggest even using (float) rather than (double) also the step 3) should use a cast,
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
char *string="1e99"
double d = atof( string ) ;
if( d > maxint ){ /* Too large */
  long i = atol(string); /* not too large */
}
/* On The Other Hand: */
char *string="9999999999999999999e-99"
double d = atof( string ) ;
if( d > maxint ){ /* Too large */
}else{
   long i=atol(string); /* also too large */
]

/* why not: */
long i=strtoul(string,0,10);
if( errno ){ perror("\n"); }
 
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
jhurst -
1. omitted the cast as we're in C (does no harm though)
2. atof returns double, hence the choice
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
after getting the nuber in the string, first check that the length of the string is shorter then the number of digits in MAXINT, then check each digit starting from the highest to the lowest by devidind MAXINT by powers of 10. this way you don't need to use anything else BUT 'int's and your string...
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
assuming the string is all digits and the length of MAXINT, you an do a strcmp
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
ozo,
why should the string be the same length of MAXINT ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Because someone said that after getting the nuber in the string, you first check that the length of the string is shorter then the number of digits in MAXINT...
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
ozo, sorry to disapoint you, but strcmp doesn't take 'const int' (such as MAXINT) as a parameter but only strings...
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
neither does strlen
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:arnond
Comment Utility
I know, but you don't need either. there are many ways to check the length of an integer. you can devide the integer by 10 time after time and when you get a 0 resault, you know the length (the number of divisions, minus one). so why use any "str..." functions ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
because "the user enters a number into a string"
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
I know, but you don't need either. there are many ways to check the length of an integer. you can devide the integer by 10 time after time and when you get a 0 resault, you know the length (the number of divisions, minus one). so why use any "str..." functions ?
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
ozo, please re-read my first entry. the process I suggested will guarantee the relation between the number in the string and MAXINT. after making sure that MAXINT is bigger, larockd can use atoi.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I never said that the procees you suggested wouldn't guarantee the relation.
I merely mentioned another process.
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
but your process doesn't work !
anyway, do you think larockd is still around ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I stated certain restrictions necessary for strcmp to work.
Did you wish to add any furthur restrictions?

I don't know whether larockd is still around.

(By the way, I think the name of the maximum value for an object of type int in <limits.h> is INT_MAX)
0
 
LVL 3

Expert Comment

by:arnond
Comment Utility
the only restrictions i've added is the use of INTs and strings.
you are right, INT_MAX is the correct name.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I thought the use if int's and strings was implicit in the question.
Why do you say that my process doesn't work?
0
 

Expert Comment

by:JHandby
Comment Utility
You could use strtol() to convert it to a long. If that fails because the number is out of range, strtol() will return LONG_MAX or LONG_MIN, so you can stop right there. If it succeeds, you just need to check the returned long against INT_MAX if sizeof(int) < sizeof(long int) on your platform.

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now