Ada out parameter

I am new to Ada.

I saw this question , but mine is a little different:

type A is record x : integer; y : integer; end record; procedure P1 is temp : A; begin temp.x := 100; P2(temp); if temp.x = 100 then Ada.Text_IO.Put_Line("true"); else Ada.Text_IO.Put_Line("false"); end if; end One; procedure P2 (arg1 : out A) is begin arg1.y := 200; end P2; 

My question is the "out" parameter in P2: will the other parts of the composite type be undefined if P2 did not explicitly set them. In other words, if P1 is called, will the output be definitely true or false? Or maybe ambiguous?

This link talks about "default initialization", but my example above does not have explicit (intentional).

  Safety is preserved by ensuring that a subcomponent does not become "deinitialized" by being passed as an out parameter. If any subcomponent of a type passed by copy has default initialization, then the whole object is copied in at the start of the call so that the value of such a subcomponent is not lost as a result of a subprogram call during which no assignment is made to the subcomponent. But in practice records are usually passed by reference anyway. 
+5
source share
1 answer

Cited run ยง6.1.1 Out Parameters , it may be easier to understand after viewing ยง6.2 Formal parameter parameters . For a parameter of type A "it is not specified whether the parameter is passed by copy or link." Implementation is free to choose. In any case, the component x value of type A does not change to P2 . P1 prints "true" because you set component x to 100 explicitly before calling P2 . In the absence of initialization, by default or otherwise, temp.x contains any bits in memory when creating space for temp , usually by setting the stack pointer.

As an exercise, try to exclude initialization and examine the meaning:

 --temp.x := 100; P2(temp); if temp.x = 100 then Ada.Text_IO.Put_Line("true"); else Ada.Text_IO.Put_Line("false"); end if; Ada.Text_IO.Put_Line(temp.x'Img & temp.y'Img); 

In my implementation, the predicate fails, and temp.x contains garbage.

 false 1934820168 200 

Using default_expression with recording components avoids the risk of ignoring initialization.

 type A is record x : integer := 0; y : integer := 0; end record; 

If it depends on the compiler, then it uses in out only sure way to make sure that it works.

There is no default initialization, yes. As noted in ยง6.1 Parameter and result mechanism , "In Ada 95, it is not erroneous to depend on the parameter transfer mechanism (by reference or by copy) for those types that both allow, although this is not portable ." Since arg1 in P2 is an out parameter that can be passed in by the copy, and it is neither an access type, nor a composite type with discriminators, nor a type having an implicit initial value - ยง6.4.1 Parameter associations clearly show that "the formal parameter is not initialized " In contrast, for the parameter in out "the value of the actual parameter ... is assigned to the formal."

+6
source

Source: https://habr.com/ru/post/1259791/


All Articles