Ada 输出参数
Ada out parameter
我是 Ada 新手。
我看到了这个question,但我的有点不同:
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;
我的问题是关于 P2 中的 "out" 参数:如果 P2 没有显式设置,复合类型的其他部分是否会未定义。换句话说,如果调用 P1,输出结果是真还是假?还是模棱两可?
这个 link 谈论 "default initialization",但我上面的例子没有明确(故意)。
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.
引用的段落,§6.1.1 Out Parameters, may be easier to understand after reviewing §6.2 Formal Parameter Modes。对于A
、"it is unspecified whether the parameter is passed by copy or by reference."类型的参数,实现方式可以自由选择。在任何一种情况下,类型 A
的值的 x
组件都不会被 P2
更改。 P1
打印 "true" 因为你在调用 P2
之前明确地给 x
组件一个值 100
。缺少初始化,默认或其他方式,temp.x
包含为 temp
腾出空间时内存中的任何位,通常通过调整堆栈指针。
作为练习,尝试省略初始化并检查值:
--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);
在我的实现中,谓词失败并且 temp.x
包含垃圾。
false
1934820168 200
将 default_expression 与记录组件一起使用可避免忽略初始化的风险。
type A is record
x : integer := 0;
y : integer := 0;
end record;
If it is compiler dependent, then is using in out
the only sure way to make sure it works.
没有默认初始化,是的。正如 §6.1 Parameter and Result Mechanism, "In Ada 95 it is not erroneous to depend on the parameter passing mechanism (by-reference versus by-copy) for those types that allow both, though it is nonportable." Because arg1
in P2
is an out
parameter that may be passed by copy—and it is neither an access type nor a composite type with discriminants nor a type having an implicit initial value—§6.4.1 Parameter Associations 中指出的那样,"the formal parameter is uninitialized." 相比之下,对于 in out
参数,"the value of the actual parameter is…assigned to the formal."
我是 Ada 新手。
我看到了这个question,但我的有点不同:
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;
我的问题是关于 P2 中的 "out" 参数:如果 P2 没有显式设置,复合类型的其他部分是否会未定义。换句话说,如果调用 P1,输出结果是真还是假?还是模棱两可?
这个 link 谈论 "default initialization",但我上面的例子没有明确(故意)。
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.
引用的段落,§6.1.1 Out Parameters, may be easier to understand after reviewing §6.2 Formal Parameter Modes。对于A
、"it is unspecified whether the parameter is passed by copy or by reference."类型的参数,实现方式可以自由选择。在任何一种情况下,类型 A
的值的 x
组件都不会被 P2
更改。 P1
打印 "true" 因为你在调用 P2
之前明确地给 x
组件一个值 100
。缺少初始化,默认或其他方式,temp.x
包含为 temp
腾出空间时内存中的任何位,通常通过调整堆栈指针。
作为练习,尝试省略初始化并检查值:
--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);
在我的实现中,谓词失败并且 temp.x
包含垃圾。
false
1934820168 200
将 default_expression 与记录组件一起使用可避免忽略初始化的风险。
type A is record
x : integer := 0;
y : integer := 0;
end record;
If it is compiler dependent, then is using
in out
the only sure way to make sure it works.
没有默认初始化,是的。正如 §6.1 Parameter and Result Mechanism, "In Ada 95 it is not erroneous to depend on the parameter passing mechanism (by-reference versus by-copy) for those types that allow both, though it is nonportable." Because arg1
in P2
is an out
parameter that may be passed by copy—and it is neither an access type nor a composite type with discriminants nor a type having an implicit initial value—§6.4.1 Parameter Associations 中指出的那样,"the formal parameter is uninitialized." 相比之下,对于 in out
参数,"the value of the actual parameter is…assigned to the formal."