【发布时间】:2017-02-05 07:38:11
【问题描述】:
所以我有一个作业说:
请使用包/类的通用实例化。每个 BMR(矩阵)的空间必须在通用包/模板内的系统堆栈中分配,可能在通用实例化期间!您特别不能使用“new”、“malloc”或任何其他运算符,它们在运行时以任何语言在堆中分配空间。用荧光笔清楚地标记代码的这一部分!您必须阅读所有事务并打印所有结果在通用包/模板中。I/O 例程必须作为通用参数传递
和不相关的代码:
generic
type subscript is (<>); --range variable to be instantiated
type myType is private; --type variable to be intantiated
package genericArray is
type userDefinedArray is array(subscript range <>) of myType;
end genericArray;
with ada.text_io; use ada.text_io;
with genericArray;
procedure useGenericArray is
type month_name is (jan, feb, mar, apr, may, jun,
jul, aug, sep, oct, nov, dec);
type date is
record
day: integer range 1..31; month: month_name; year: integer;
end record;
type family is (mother, father, child1, child2, child3, child4);
package month_name_io is new ada.text_io.enumeration_io(month_name);
use month_name_io;
package IIO is new ada.text_io.integer_io(integer);
use IIO;
package createShotArrayType is new genericArray(family, date);
use createShotArrayType;
vaccine: userDefinedArray(family);
begin
vaccine(child2).month := jan;
vaccine(child2).day := 22;
vaccine(child2).year := 1986;
put("child2: ");
put(vaccine(child2).month);
put(vaccine(child2).day);
put(vaccine(child2).year); new_line;
end useGenericArray;
同样,发布的代码与分配无关,但我的问题是,在发布的代码中,每次我使用“新”这个词时,是在堆栈还是堆中分配空间。因为我的指示说不要使用这个词,但是它说要使用我认为需要它的通用实例化。我将不胜感激!
【问题讨论】:
-
不知道你是不是和this question上的课一样?如果这些问题中的任何一个要作为重复关闭,我认为应该是另一个!
-
我认为你是对的。据我所知,未来作业的所有方向都写得这么差。还好我找到了这个网站。 @西蒙·赖特
标签: generics heap-memory instantiation ada stack-memory