Re: Checking size of variables (Henry Spencer)
18 May 2005 12:17:12 -0400

          From comp.compilers

Related articles
Checking size of variables (Thomas Christensen) (2005-05-16)
Re: Checking size of variables (Karsten Nyblad) (2005-05-18)
Re: Checking size of variables (Hans-Peter Diettrich) (2005-05-18)
Re: Checking size of variables (2005-05-18)
Re: Checking size of variables (2005-05-18)
Re: Checking size of variables (Peter \Firefly\Lund) (2005-08-07)
| List of all articles for this month |

From: (Henry Spencer)
Newsgroups: comp.compilers
Date: 18 May 2005 12:17:12 -0400
Organization: SP Systems, Toronto, Canada
References: 05-05-149
Keywords: analysis, errors
Posted-Date: 18 May 2005 12:17:12 EDT

Thomas Christensen <> wrote:
>For example. If a user declares a variable as an "int" in the source
>code, and assigns it some 50 digit number (or any number, exceeding
>the size of an int).
>Is this sort of error detected by the lexer or by the type checker, or ?

Most likely it would happen in one of four ways:

+ The scanner/lexer, trying to convert the number to an internal
representation (either that of the type implied by the form of the
constant, or a worst-case temporary internal type), finds it won't fit.

+ The type checker, trying to determine the type of the constant, finds
that the largest possible integer type isn't big enough.

+ The type checker successfully assigns the constant a type, but then
finds that it's not assignment-compatible with the variable, because the
language doesn't have an implicit narrowing conversion.

+ Either the type checker, or some later phase attempting to evaluate
expressions at compile time, notices that the implicit narrowing
conversion would inevitably overflow.

It's also possible for the compiler to accept the code, with the overflow
caught only at run time.

And in sloppy languages like C, it's quite possible for this to be valid
code, or invalid code that the implementation is not required to catch.
"Think outside the box -- the box isn't our friend." | Henry Spencer
                                                                -- George Herbert |

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.