提问人:A.Pissicat 提问时间:9/27/2023 最后编辑:A.Pissicat 更新时间:9/28/2023 访问量:76
从地址初始化类型
Initialise type from address
问:
我想从 .例如:System.address
generic
type T_Generic is private;
package MyPackage is
G_Addr : System.Address;
procedure Register (myAddr : System.address);
procedure MyProcedure (myAddr : System.address);
end MyPackage;
package body MyPackage is
procedure Register(myAddr : System.address)
is
G_Addr := myAddr;
end MyProcedure;
procedure MyProcedure (myAddr : System.address)
myVar : T_Generic with address => myAddr;
is
if (myAddr = G_Addr)
then
-- work with myVar
end if;
end MyProcedure;
end MyPackage;
但是我无法编译这段代码:myVar : T_Generic with address => myAddr;
方面规范是 Ada 2012 的一项功能
单元必须使用 -gnat2012 开关编译
我无法更改编译的选项,我该怎么办?
答:
1赞
Shark8
9/28/2023
#1
但是我无法编译这段代码:
myVar : T_Generic with address => myAddr;
方面规范是 Ada 2012 的一项功能
单元必须使用 -gnat2012 开关编译
My_Address : Constant System.Address:= To_Address( 16#DEAD_BEEF# );
My_Variable : Whatever;
For My_Variable'Address use My_Address;
评论
0赞
Jim Rogers
10/2/2023
当然,为某种类型的变量分配地址并不能确保变量包含有效值。例如,变量类型的大小可能与周围内存中的数据对象不一致。我想知道 A.Pissicat 试图实现什么。使用访问类型会更安全,而不是简单地将内存位置分配给随机类型。
评论
myVar : T_Generic; for myVar'Address use myAddr;