From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8c07fb80fc02e8b0 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!out02a.usenetserver.com!news.usenetserver.com!in01.usenetserver.com!news.usenetserver.com!in03.usenetserver.com!news.usenetserver.com!pc02.usenetserver.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Newbie question References: From: Stephen Leake Date: Sat, 24 Nov 2007 11:49:04 -0500 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/22.1 (windows-nt) Cancel-Lock: sha1:0YAJjjhfTYiHks/fTLLF+2A3Y3w= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: d9975474855fde05e48ed21504 Xref: g2news1.google.com comp.lang.ada:18605 Date: 2007-11-24T11:49:04-05:00 List-Id: Sir Chewbury Gubbins 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