comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Newbie question
Date: Sat, 24 Nov 2007 11:49:04 -0500
Date: 2007-11-24T11:49:04-05:00	[thread overview]
Message-ID: <uejefzj0f.fsf@stephe-leake.org> (raw)
In-Reply-To: gq0j15-ga6.ln1@isis.home

Sir Chewbury Gubbins <chewbury.gubbins@nelefa.org.invalid> writes:

> Good afternoon! I apologise if this is an eye-rolly question, but
> I'm having a bit of an issue using Gnat with the Lovelace tutorial.
>
> A trivial example - the "Compute" procedure on:
>
> http://www.adahome.com/Tutorials/Lovelace/s1sf.htm

Here's a modified version, that always exits the loop:

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use 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.

   I : Integer := 1; -- loop counter
begin -- procedure Compute
   loop
      I := I + 1;
      exit when I > 50;
      Put (X);
      New_Line;
      Double (X);
   end loop;
end Compute;

If you compile this with the default GNAT options, you get:

bash-3.2$ stephe@LM000850872$ gnatmake compute
gcc -c compute.adb
gnatbind -x compute.ali
gnatlink compute.ali
stephe@LM000850872$ ./compute
          1
          2
          4
          8
         16
         32
         64
        128
        256
        512
       1024
       2048
       4096
       8192
      16384
      32768
      65536
     131072
     262144
     524288
    1048576
    2097152
    4194304
    8388608
   16777216
   33554432
   67108864
  134217728
  268435456
  536870912
 1073741824
-2147483648
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
          0
stephe@LM000850872$ 

If you compile with -gnato, you get:

stephe@LM000850872$ rm *.o
stephe@LM000850872$ gnatmake compute -gnato
gcc -c -gnato compute.adb
gnatbind -x compute.ali
gnatlink compute.ali
stephe@LM000850872$ ./compute
          1
          2
          4
          8
         16
         32
         64
        128
        256
        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:7 overflow check failed
stephe@LM000850872$ 

> If I stick the infinite loop back in, I don't get the expected
> series of doublings, but just an infinite number of zeros.

I suspect you missed the initial counting, because it scrolls off the
screen so quickly. Another option is to add a "delay 1.0;" to the
loop, so you can see it count.

One point of this excercise, as the questions following it show, is to
demonstrate that Ada raises exceptions when things go wrong, instead
of silently doing the wrong thing.

However, as has been pointed out many times here, GNAT with the
default options is _not_ strictly an Ada compiler. You need -gnato and
-fstack-check to get strict Ada compliance.

-gnato tells GNAT to check for constraint errors. It is left off by
default for historical reasons; the initial reason, back in the dawn
of GNAT, was the notion that such checks would make the code "too
slow". Many have argued that was a bad idea (and I agree :), but
AdaCore is not going to change the default now.

The other lesson here is that you _must_ read the user's guide for a
compiler before using it for serious work (or even non-serious work).
In particular, you _must_ understand the compiler options, and
deliberately decide whether to keep the defaults or override them.

-- 
-- Stephe



  parent reply	other threads:[~2007-11-24 16:49 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-23 13:22 Newbie question Sir Chewbury Gubbins
2007-11-23 14:01 ` Sir Chewbury Gubbins
2007-11-24 16:49 ` Stephen Leake [this message]
2007-11-24 17:08   ` Peter C. Chapin
2007-11-25 19:25     ` Stephen Leake
2007-11-29  0:46   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
2009-03-12 13:29 Olivier Scalbert
2009-03-12 13:48 ` Ludovic Brenta
2009-03-12 14:16   ` Olivier Scalbert
2009-03-12 14:31     ` Ludovic Brenta
2009-03-12 14:32     ` stefan-lucks
2009-03-12 14:36       ` Ludovic Brenta
2009-03-12 17:14         ` stefan-lucks
2009-03-12 18:29           ` Adam Beneschan
2009-03-12 15:03       ` Jacob Sparre Andersen
2009-03-12 15:07   ` Robert A Duff
2009-03-12 17:16 ` Georg Bauhaus
2009-03-13  1:59 ` tmoran
2009-03-15 13:46 ` Olivier Scalbert
2009-03-21 18:08 ` Olivier Scalbert
2009-03-21 18:22   ` (see below)
2009-03-21 18:29     ` Olivier Scalbert
2009-03-21 18:36       ` Georg Bauhaus
2009-03-21 18:39         ` Olivier Scalbert
2009-03-22 12:40           ` (see below)
2009-03-22 13:19             ` Olivier Scalbert
2009-03-21 18:39       ` Jeffrey R. Carter
2009-03-23  8:24       ` Jean-Pierre Rosen
2005-07-18 11:26 Francisco J. Montoya
2005-07-18 13:14 ` Martin Dowie
2005-07-18 13:51   ` Alex R. Mosteo
2005-07-18 20:27     ` Martin Dowie
2005-07-18 15:28 ` Jeffrey Carter
2005-07-18 17:40 ` Martin Krischik
2005-07-03 17:58 newbie question e.coli
2005-07-03 18:32 ` Dmitry A. Kazakov
2004-08-04  6:42 Newbie Question leke
2004-08-04  8:55 ` Frank
2004-08-04  9:51 ` Martin Dowie
2001-12-24 13:52 Newbie question Jasbinder S  Uppal
2001-12-24 20:06 ` Michal Nowak
2001-12-24 21:13   ` martin.m.dowie
2001-12-25 12:36     ` Michal Nowak
2001-12-27 14:25       ` Alfred Hilscher
2001-12-29 21:54         ` Michal Nowak
2001-12-31 17:51           ` Jasbinder S Uppal
2002-01-01 21:26             ` Michal Nowak
2000-05-25  0:00 olsonm76
2000-05-26  0:00 ` Robert Dewar
1999-03-18  0:00 newbie question Kenneth Lee
1999-03-18  0:00 ` Andreas Winckler
1999-03-19  0:00   ` Kenneth Lee
1999-03-19  0:00     ` Michael F Brenner
1999-03-19  0:00       ` ELMO
1999-03-21  0:00       ` Matthew Heaney
1999-03-18  0:00 ` Tom Moran
1999-03-18  0:00   ` Kenneth Lee
1999-03-18  0:00     ` Nick Roberts
1999-03-19  0:00 ` robert_dewar
1999-03-19  0:00 ` Michael F Brenner
1999-03-19  0:00   ` Nick Roberts
1999-03-20  0:00     ` Nick Roberts
replies disabled

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