By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Checking if number is bigger than INT MAX

P: n/a
Hi all,

I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }

Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?

Cheers,

Paulo Matos

Dec 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
po******@gmail.com wrote:
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }
"flex" is off-topic, so without knowing what 'yylval' is or how its member
'num' is declared, it's hard to say. However, since you're using the C
function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
long'. If it isn't, you need to supply more information.
Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?


It is implementation-specific, most likely, but check 'yylval.num' against

(unsigned long)INT_MAX

instead. Whether it buys you anything or not, is for you to decide. You
could also try to convert 'yytext' to an implementation-specific integral
type that is known to accommodate a larger range than 'int' and check its
value against (that_type)INT_MAX. I here used 'unsigned long' as that
type, and your system may have something better.

V
Dec 12 '05 #2

P: n/a

Victor Bazarov wrote:
po******@gmail.com wrote:
I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?

In the flex side I have:
{numeral} { yylval.num = strtoul(yytext, NULL, 0);
return NUMERAL; }


"flex" is off-topic, so without knowing what 'yylval' is or how its member
'num' is declared, it's hard to say. However, since you're using the C
function 'strtoul', I'll assume that 'yylval.num' is of type 'unsigned
long'. If it isn't, you need to supply more information.
Is there a straightforward way to check if the number in yytext is
bigger than INT_MAX?


It is implementation-specific, most likely, but check 'yylval.num' against

(unsigned long)INT_MAX


You might consider the "more C++y" static_cast<unsigned
long>(std::numeric_limits<int>::max())

Dec 12 '05 #3

P: n/a
po******@gmail.com wrote:
Hi all,

I'm doing parsing with flex and bison and I read numbers which are just
a sequence of digits [0-9]+ and keep them in an int, however if the
number is bigger than int, I should output error. What's the best way
to check this?


Easy: Strip the leading '0' from the string, get INT_MAX as a string
without leading zeroes. Compare lengths. If AsString(INT_MAX)
is longer it will fit. If AsString(INT_MAX) is shorter, it won't. If
the
lengths are equal, you can do a string compare and it will fit if
AsString(INT_MAX) < digit_sequence_without_leading_zeroes.

HTH,
Michiel Salters

Dec 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.