4 ms·
> [about Ada] You never miss worrying about whether a parameter is passed by value or reference Uh? I thought that Ada suffered from the "aliasing issue"(1): g
by renox 2y ago
> [about Ada] You never miss worrying about whether a parameter is passed by value or reference
Uh? I thought that Ada suffered from the "aliasing issue"(1): given that the compiler choose whether to copy or alias, there is a risk that the semantic of your program change when the compiler/optimisation change, am I wrong?
1: if you have a function with two parameters, one "in" and the other "inout" and you pass the same variable to the two parameters.
- tgv 2y agoYou have to specifically state when parameters can be aliased. Otherwise, the compiler forbids it.
- renox 2y agoWell godbolt doesn't think so.. 1) this example has an aliasing, no compiler error. https://godbolt.org/z/hzWfj37ef 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; <<
- tgv 2y agoMy 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.
- renox 2y agoTry 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..
- adrian_b 2y agoThe 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.
- renox 2y agoSo 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?
- adrian_b 2y agoEven if the aliasing were not forbidden by default by the compiler, aliasing in Ada would have caused problems more seldom than in a programming language that allows the programmer to specify whether parameters are passed by reference. The compiler may choose to pass by value the "in out" parameters in which case their aliasing may have no effect ("in out" parameters that are passed by value, typically in order to be passed in registers, are loaded upon function entry and stored back upon function exit). In Rust, C or any other language with parameters passed by reference, you can always alias 2 parameters passed by reference, unless the language defines that as an error that must be caught at compile-time or at run-time. Usually aliasing will lead to errors if at least one of the 2 parameters is modified inside the function. Because of this, aliased parameters which may be modified inside a function and which are not allowed explicitly shall always be flagged as an error.