gpt4 book ai didi

用于初始化动态分配数组的 Ada 语法

转载 作者:行者123 更新时间:2023-12-05 08:27:28 25 4
gpt4 key购买 nike

在 Ada 中初始化动态分配数组的正确语法是什么?我试过这个:

type Short_Array is array (Natural range <>) of Short;
Items : access Short_Array;
Items := new Short_Array(1..UpperBound)'(others => 0);

这会导致编译器错误 - “应为二元运算符”。还有这个:

type Short_Array is array (Natural range <>) of Short;
Items : access Short_Array;
Items := new Short_Array(1..UpperBound);
Items.all := (others => 0);

这似乎令人惊讶地引发了 SEGFAULT。不确定那里发生了什么,但想在开始追尾之前弄清楚语法。

最佳答案

如果您使用的是 Ada2012,您可以执行以下操作:

type Short_Array is array(Natural range <>) of Short with
Default_Component_Value => 0;
Items : access Short_Array := new Short_Array(1..UpperBound);

Ada 2012 基本原理 http://www.ada-auth.org/standards/12rat/html/Rat12-2-6.html 的第 2.6 节解释了数组默认初始值的使用。

关于用于初始化动态分配数组的 Ada 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40923154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com