Domanda

Complete beginner to Ada here. I am trying to compile and run a simple Ada program, from here: http://www.dwheeler.com/lovelace/s1sf.htm

Here is the code:

-- 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;

I'm on Linux, using gnat so I do:

gnatmake -c compute.adb
gnatmake compute

Which gives me the executable. Running the executable gives a list of zeros, as it seems to initialize X to 0, even though it says to initialize it to 1, so I should get a list 1,2,4,...

Can anyone explain either where my code or my thinking is wrong? Oh and using gnat is there a way to compile and create the executable in a single command?

È stato utile?

Soluzione

I can only guess that when you added "-gnato", gnatmake simply replied gnatmake: "compute" up to date. leaving you with the same executable.

brian@Gannet:~/Ada/Play$ gnatmake -gnato compute.adb
brian@Gannet:~/Ada/Play$ ./compute
          1
          2
...
  536870912
 1073741824
raised CONSTRAINT_ERROR : compute.adb:9 overflow check failed

Then without -gnato (I had to touch the source or I got the "up to date" message)

brian@Gannet:~/Ada/Play$ gnatmake compute.adb
brian@Gannet:~/Ada/Play$ ./compute
          1
          2
...
  536870912
 1073741824
-2147483648
          0
          0
          0
          0

which subsequently appears as a string of zeroes. Adding your extra "if" statement touched the file, forcing recompilation : the "if" itself is not strictly necessary (though testing and preventing constraint error is a Good Thing!)

The moral : without -gnato, or rather, without at least the flags -gnataoE -fstack_check, Gnat is not an Ada compiler.

Altri suggerimenti

As you double the integer value each time without any delay in the loop, it will overflow quickly. In GNAT, overflow checking is off by default, you can enable it with the -gnato switch. So when the integer value overflows, it finally gets 0 and that's what you're seeing (the output is too fast to see the initial numbers).

Compiling with -gnato should give you a constraint error on execution. You can also produce the executable with one line by executing

gnatmake -gnato compute.adb
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top