Ada out参数

T N*_*yen 5 parameters ada out

我是阿达的新手.

我看到了这个问题,但我的情况有点不同:

    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;
Run Code Online (Sandbox Code Playgroud)

我的问题是P2中的"out"参数:如果P2没有明确设置它们,复合类型的其他部分将是未定义的.换句话说,如果调用P1,输出肯定是真还是假?或者可能含糊不清?

这个链接谈到"默认初始化",但我上面的例子没有明确地(有意).

    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.
Run Code Online (Sandbox Code Playgroud)

tra*_*god 6

在查看§6.2形式参数模式后,引用的段落§6.1.1输出参数可能更容易理解.对于类型的参数,"未指定参数是通过复制还是通过引用传递." 实施可自由选择.在任何一种情况下,类型值的组件都不变.打印"true",因为您在调用之前已为组件指定了显式值.缺少初始化,默认或其他方式,通常通过调整堆栈指针来包含为空间腾出的内存中的所有位.AxAP2P1x100P2temp.xtemp

作为练习,尝试省略初始化并检查值:

--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);
Run Code Online (Sandbox Code Playgroud)

在我的实现中,谓词失败并temp.x包含垃圾.

false
 1934820168 200
Run Code Online (Sandbox Code Playgroud)

default_expression与记录组件一起使用可避免忽略初始化的风险.

type A is record
    x : integer := 0;
    y : integer := 0;
end record;
Run Code Online (Sandbox Code Playgroud)

如果它是编译器相关的,那么使用in out唯一可靠的方法来确保它的工作原理.

缺少默认初始化,是的.正如§6.1参数和结果机制中所述,"在Ada 95中,对于那些允许两者的类型,依赖参数传递机制(by-referenceby-copy)并不是错误的,尽管它是不可移植的." 因为在是该参数被传递通过复制 -and它既不的接入类型,也不是一个复合型与判别也不具有一个隐含的初始值-类型§6.4.1参数协会明确指出"形式参数是未初始化的. " 相反,对于参数,"实际参数的值是...... 分配给形式的".arg1P2outin out