Re: Checking size of variables (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
18 May 2005 00:50:50 -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: (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.compilers
Date: 18 May 2005 00:50:50 -0400
Organization: Department of Computer Science, University of Copenhagen
References: 05-05-149
Keywords: errors
Posted-Date: 18 May 2005 00:50:50 EDT

"Thomas Christensen" <> writes:

> Where does a compiler _usually_ perform checks on the size of
> integers and strings ?
> 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 ?

It depends on the language. Some languages allow integers of
different size, so at best you can only complain about integers
exceeding the maximum size during lexing or parsing. It is only
during type-checking or maybe even not until runtime that you can see
if an actual value exceeds the size if the variable into which it is
stored. Some languages/compilers allow floating point constants to be
expressed as large integer constants, so an integer constant of 50
digits could be used where a floating point number is expected (of
course, some precision will be lost).

Other languages (like C) treat integers as modulo the wordsize, so a
50-digit constant would theoretically be valid, but only the last 32
or 64 bits would be stored.

I guess the safest way of handling constants is to leave them as
strings until code generation time, where you must decide on how to
represent it on the machine in question and give an error if you can
not. Or, even better, have unbounded integers as a standard datatype,
so conversion to bounded integers or floating point numbers is done by
an explicit or implicit typecast in the program.


Post a followup to this message

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