Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well godbolt doesn't think so.. 1) this example has an aliasing, no compiler error. https://godbolt.org/z/hzWfj37ef

2) The output value depends of the size of IntArray!

>> with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Text is type IntArray is array (1 .. 500) of Integer;

   type MyStruct is record
      Value : Integer;
       A : IntArray := (others => 0);
   end record;

   function Compute (Value1 : MyStruct; Value2 : in out MyStruct) return Integer is
   begin
      Put_Line ("1 value1=" & Integer'Image(Value1.Value) & " value2=" & Integer'Image(Value2.Value));
      Value2.Value := Value2.Value - 1;
      Put_Line ("2 value1=" & Integer'Image(Value1.Value) & " value2=" & Integer'Image(Value2.Value));
      return Value1.Value * 2;
   end Compute;

   Value1 : MyStruct := (Value => 10, A => (others => 0));
   Value2 : MyStruct := (Value => 20, A => (others => 0));
   Result1 : Integer;
   Result2 : Integer;
begin Result1 := Compute(Value1, Value2); Put_Line ("a value1=" & Integer'Image(Value1.Value) & " value2=" & Integer'Image(Value2.Value) & " result1=" & Integer'Image(Result1)); Result2 := Compute(Value1, Value1); Put_Line ("b value1=" & Integer'Image(Value1.Value) & " value2=" & Integer'Image(Value2.Value) & " result2=" & Integer'Image(Result2)); end Text; <<


The SPARK Ada tools detect this as an error, but not the free Ada compiler.

This should have also been detected as an error by the normal Ada compiler, but I assume that this is avoided in order to enforce market segmentation.

Those who are not happy with the reduced error detection abilities of the free Ada GNAT compiler are expected to pay for SPARK.

I think that the main reason why the use of Ada has remained restricted is that even today having access to complete Ada development tools is expensive, even if the free GNAT tools are enough when you do not need the better error detection provided by the paid tools.


So in this case C or C++ is safer than Ada (if you use GNAT) but at the price of reduced performance, funny no?

Note that at some point Zig had the same semantic as Ada, but then they changed to use C's (less efficient) semantic: this aliasing detection must be difficult to do..

Is there an online Ada compiler which detect the issue?


My Ada is somewhat weak, but IIRC Value2 doesn't alias Value1. The parameters don't work like C. It's more like an assignment once the function returns, although the compiler is free to deviate from that.


Try it: change the size of the array: with a small array, it's passed by value, with a big array it's passed by reference and the value of "result2" is changed..




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: