comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeff C," <nolongersafeto@userealemailsniff.com>
Subject: Re: Getting valid Integer values !!
Date: Wed, 22 Oct 2003 03:46:14 GMT
Date: 2003-10-22T03:46:14+00:00	[thread overview]
Message-ID: <a4nlb.201306$%h1.199852@sccrnsc02> (raw)
In-Reply-To: mailman.161.1066792253.25614.comp.lang.ada@ada-france.org

RE: Getting valid Integer values !!
"Beard, Frank Randolph CIV" <frank.beard@navy.mil> wrote in message
news:mailman.161.1066792253.25614.comp.lang.ada@ada-france.org...
-----Original Message-----
From:   CheGueVerra [mailto:chegueverra@hotmail.com]

>>   type IntData_Type is range 1000000..9999999;
>>   IntData  : IntData_Type := IntData_Type'first;

> would the last line put the value of 1000000 in IntData ?

Yes.

It's always good to initialize your variables, especially
when they have a resticted range.  There is no guarantee the
variable will contain valid data after the space is allocated.
You could get what ever garbage data was left in that memory
location.  If the data happens to be out of range and you
reference it (such as passing the variable into a routine,
etc) CONSTRAINT_ERROR will be raised.

Frank





Funny..I often hear the "Initalize all variables" things but I could not
disagree more! (kind of :)

The problem is that unless you are initializing it to a value that makes
sense in the problem
solution domain (e.g. Initilize array index to 'first for some interesting
loop construct) I believe
you are hurting yourself more than you are helping. The reason is that if
you do  not initialize it and
you get to a segment of the code where you try to use it there are several
good things (one of which
you list as a bad thing) that can happen

1) The compiler warns you that you are reading from a variable before
writing to it.
2) Your cool static analyzer program detects that you are reading from a
variable before writing it.
     (http://www.polyspace.com/product_datasheet/adaverifier.htm)
3) Your human code reviewers detect that you are reading from a variable
before writing it.
4) You are smart and use something like GNATs pragma normalize_scalars or
Initialize_Scalars to force
     the compiler to initialize the variables to invalid values so you can
get (and eliminate) constraint errors during
     test and debug.
(http://www.cs.kuleuven.ac.be/~dirk/papers/ae02cfmu-paper.pdf)

If you initialize everything you do get the added benifit that your program
will probably tend to run
the same (even if it is wrong (e.g. 'first might be a valid number not not
really make sense for how you now intend to use the variable)) ) more often
but I think the losses outweight the benefits because even the same
"wrongness" could go
undetected for years until provided with the magic killer input data.

I do agree that if you can reasonable initialize the variable to the value
that makes sense for its intended use at time of declaration that you have
done a very good thing. But just initializing to some fixed "in range" value
is of marginal use.








       reply	other threads:[~2003-10-22  3:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.161.1066792253.25614.comp.lang.ada@ada-france.org>
2003-10-22  3:46 ` Jeff C, [this message]
2003-10-22 19:27   ` Getting valid Integer values !! Simon Wright
2003-10-21 20:34 Beard, Frank Randolph CIV
2003-10-21 21:34 ` CheGueVerra
2003-10-21 21:36 ` CheGueVerra
  -- strict thread matches above, loose matches on Subject: below --
2003-10-21 19:11 CheGueVerra
2003-10-21 20:15 ` Martin Dowie
2003-10-21 20:27   ` CheGueVerra
2003-10-21 23:04     ` Martin Dowie
2003-10-21 23:35       ` CheGueVerra
2003-10-22  2:49         ` Steve
2003-10-22  0:46     ` Jeffrey Carter
2003-10-22  1:07       ` CheGueVerra
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox