Solved

Checking For An Integer Out Of Range

Posted on 1998-09-29
20
275 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
ID: 1253108
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
ID: 1253109
I would suggest even using (float) rather than (double) also the step 3) should use a cast,
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253110
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 8

Expert Comment

by:Answers2000
ID: 1253111
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
ID: 1253112
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
ID: 1253113
assuming the string is all digits and the length of MAXINT, you an do a strcmp
0
 
LVL 3

Expert Comment

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

Expert Comment

by:ozo
ID: 1253115
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
ID: 1253116
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
ID: 1253117
neither does strlen
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253118
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
ID: 1253119
because "the user enters a number into a string"
0
 
LVL 3

Expert Comment

by:arnond
ID: 1253120
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
ID: 1253121
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
ID: 1253122
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
ID: 1253123
but your process doesn't work !
anyway, do you think larockd is still around ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253124
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
ID: 1253125
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
ID: 1253126
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
ID: 1253127
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

831 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