Ada Generics:堆栈与堆澄清
Ada Generics: Stack vs Heap Clarification
所以我有一个作业说:
请使用 packages/classes 的通用实例化。 Space 对于每个 BMR(矩阵),必须在泛型 package/template 内的系统堆栈中分配,可能是在泛型实例化期间!您特别不得使用“new”、“malloc”或任何其他运算符,这些运算符会在运行时以任何语言在堆中分配 space。用荧光笔清楚地标记您的代码的这一部分!您必须阅读所有事务并打印通用 package/template 中的所有结果。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;
同样,发布的代码与赋值无关,但我的问题是,在发布的代码中,每次我使用 space 这个词时,是否在堆栈或堆中分配了 space =23=]。因为我的指示说不要使用这个词,但后来它说要使用我认为需要它的通用实例化。我将不胜感激!
说明说
You specifically may not use "new,” “malloc,” or any other operator, which allocates space in the heap at runtime in any language.
这显然意味着你不能进行堆分配(为什么他们不能先这么说我不知道;可能更清楚)。 "operator" 之后的逗号具有误导性。
泛型的实例化通常发生在编译时;但即使你做到了
Ada.Integer_IO.Get (N);
declare
package Inst is new Some_Generic (N);
begin
...
end;
实例化本身不涉及堆分配。
您可以编写泛型,以便上面的实例化分配堆栈:
generic
J : Positive;
package Some_Generic is
type Arr is array (1 .. J) of Integer;
A : Arr; -- in a declare block, this ends up on the stack
end Some_Generic;
甚至堆:
generic
J : Positive;
package Some_Generic is
type Arr is array (1 .. J) of Integer;
type Arr_P is access Arr;
P : Arr_P := new Arr; -- always on the heap
end Some_Generic;
但那是因为你在泛型中编写的代码,而不是要求你使用单词 new
来实例化它的语法。
所以我有一个作业说:
请使用 packages/classes 的通用实例化。 Space 对于每个 BMR(矩阵),必须在泛型 package/template 内的系统堆栈中分配,可能是在泛型实例化期间!您特别不得使用“new”、“malloc”或任何其他运算符,这些运算符会在运行时以任何语言在堆中分配 space。用荧光笔清楚地标记您的代码的这一部分!您必须阅读所有事务并打印通用 package/template 中的所有结果。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;
同样,发布的代码与赋值无关,但我的问题是,在发布的代码中,每次我使用 space 这个词时,是否在堆栈或堆中分配了 space =23=]。因为我的指示说不要使用这个词,但后来它说要使用我认为需要它的通用实例化。我将不胜感激!
说明说
You specifically may not use "new,” “malloc,” or any other operator, which allocates space in the heap at runtime in any language.
这显然意味着你不能进行堆分配(为什么他们不能先这么说我不知道;可能更清楚)。 "operator" 之后的逗号具有误导性。
泛型的实例化通常发生在编译时;但即使你做到了
Ada.Integer_IO.Get (N);
declare
package Inst is new Some_Generic (N);
begin
...
end;
实例化本身不涉及堆分配。
您可以编写泛型,以便上面的实例化分配堆栈:
generic
J : Positive;
package Some_Generic is
type Arr is array (1 .. J) of Integer;
A : Arr; -- in a declare block, this ends up on the stack
end Some_Generic;
甚至堆:
generic
J : Positive;
package Some_Generic is
type Arr is array (1 .. J) of Integer;
type Arr_P is access Arr;
P : Arr_P := new Arr; -- always on the heap
end Some_Generic;
但那是因为你在泛型中编写的代码,而不是要求你使用单词 new
来实例化它的语法。