comp.lang.ada
 help / color / mirror / Atom feed
* Getting started with Ada: Runtime exceptions?
@ 2001-11-28  0:47 Dick Rumsfeld
  2001-11-28  1:29 ` Jeffrey Carter
  0 siblings, 1 reply; 2+ messages in thread
From: Dick Rumsfeld @ 2001-11-28  0:47 UTC (permalink / raw)


Hello all,
I've just today started to learn Ada. In Section 1.4 of the online
tutorial I am learning from,
http://www.adahome.com/Tutorials/Lovelace/s1sf.htm
it is stated that the below program, when run, will print 2^n for each
n starting with n=0, until an overflow occurs, at which time the
program will automatically halt with a message stating an exception
occured. Well, when I compiled and ran the below program using
"gnatmake" (which I've just downloaded for my Linux distribution), it
gives the following output:

          1
          2
          4
          8
       [...output elided...]
  536870912
 1073741824
-2147483648
          0
          0
          0
         ...

This looks more like the behavior I would expect from C. So then, what
do I need to do to get what I presume from the tutorial to be the
proper exception generating behavior?

Here is the program:

-- Demonstrate a trivial procedure, with another nested inside.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
 
procedure Compute is

 procedure Double(Item : in out Integer) is
 begin -- procedure Double.
   Item := Item * 2;
 end Double;

 X : Integer := 1;   -- Local variable X of type Integer.

begin -- procedure Compute
 loop
  Put(X);
  New_Line;
  Double(X);
 end loop;
end Compute;



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Getting started with Ada: Runtime exceptions?
  2001-11-28  0:47 Getting started with Ada: Runtime exceptions? Dick Rumsfeld
@ 2001-11-28  1:29 ` Jeffrey Carter
  0 siblings, 0 replies; 2+ messages in thread
From: Jeffrey Carter @ 2001-11-28  1:29 UTC (permalink / raw)


Dick Rumsfeld wrote:
> 
> Hello all,
> I've just today started to learn Ada. In Section 1.4 of the online
> tutorial I am learning from,
> http://www.adahome.com/Tutorials/Lovelace/s1sf.htm
> it is stated that the below program, when run, will print 2^n for each
> n starting with n=0, until an overflow occurs, at which time the
> program will automatically halt with a message stating an exception
> occured. Well, when I compiled and ran the below program using
> "gnatmake" (which I've just downloaded for my Linux distribution), it
> gives the following output:
> 
>           1
>           2
>           4
>           8
>        [...output elided...]
>   536870912
>  1073741824
> -2147483648
>           0
>           0
>           0
>          ...

A dirty little secret about GNAT is that integer overflow checking is
OFF by default, as you discovered. If you had read the Secret
Documentation for GNAT, you'd know that you turn it ON with the switch

-gnato

in which case your program should output

...
        512
       1024
       2048
       4096
       8192
      16384
      32768
      65536
     131072
     262144
     524288
    1048576
    2097152
    4194304
    8388608
   16777216
   33554432
   67108864
  134217728
  268435456
  536870912
 1073741824

raised CONSTRAINT_ERROR : compute.adb:9

which is much less C-like than your experience. I hope this foible of
GNAT will not put you off Ada.

This default was chosen because, IIRC, in GNAT's early days integer
overflow checking was very expensive on some platforms. I don't know if
that's still the case, but modern processors are so much faster than
those of GNAT's early days that there are probably few situations in
which it makes a difference, so this is probably still the default only
for compatibility reasons. I certainly always turn overflow checking on
when using GNAT, and have never found it too slow.

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-11-28  1:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-28  0:47 Getting started with Ada: Runtime exceptions? Dick Rumsfeld
2001-11-28  1:29 ` Jeffrey Carter

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