Thanks in advance for any help. I am currently doing some beginner work on ada programming and I have installed GNAT Programming Studio (GPS) from http://libre.adacore.com/download/configurations#
I have Windows 10 64-bits. I was given the following code at school:
pragma Task_Dispatching_Policy(FIFO_Within_Priorities);
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;
procedure PeriodicTasks is
Start : Time;
package Duration_IO is new Ada.Text_IO.Fixed_IO(Duration);
package Int_IO is new Ada.Text_IO.Integer_IO(Integer);
task type T(Id: Integer; Period : Integer) is
pragma Priority(Id);
end;
task body T is
Next : Time;
X : Integer;
begin
Next := Start;
loop
Next := Next + Milliseconds(Period);
-- Some dummy function
X := 0;
for Index in 1..5000000 loop
X := X + Index;
end loop;
Duration_IO.Put(To_Duration(Clock - Start), 3, 3);
Put(" : ");
Int_IO.Put(Id, 2);
Put_Line("");
delay until Next;
end loop;
end T;
-- Example Task
Task_P10 : T(10, 250);
Task_P12 : T(12, 500);
Task_P14 : T(14, 500);
Task_P16 : T(16, 250);
Task_P18 : T(18, 500);
Task_P20 : T(20, 250);
begin
Start := Clock;
null;
end PeriodicTasks;
Until recently, GNAT didn't check for integer overflow by default. It did check for constraint errors, e.g. assigning 0 to a Positive
.
Many of us thought this was a strange choice by the compiler developers, because it led to many questions whose root cause was failure to handle integer overflow. The recent change leads us to suppose that the developers now agree!
Your problem arises because of the statement
for Index in 1..5000000 loop
X := X + Index;
end loop;
which would end up with X ~ 10^13, which doesn’t fit in a 32-bit integer (it would fit in a 64-bit integer, but that would be a Long_Long_Integer
on most if not all GNAT platforms).
It’s likely that your Windows compiler is GNAT GPL 2016, which shows the new behaviour, while the Ubuntu compiler is an older FSF GCC.
You can tell your Windows compiler to use the old behaviour using the compiler switch -gnato0
.
You can tell your Ubuntu compiler to use the new behaviour using the compiler switch -gnato
.