访问类型声明对释放的影响

Access type declaration effect on deallocation

在两种情况下,在声明块之后(当然是在过程结束之前)释放内存的方式是否相同:

procedure allocation is 
    type T_Integer_Access is access Integer;
begin
    declare
        P : T_Integer_Access;
    begin
        P := new Integer'(5);
    end;
end;

procedure allocation is 
begin
    declare
        type T_Integer_Access is access Integer;
        P : T_Integer_Access;
    begin
        P := new Integer'(5);
    end;
end;

换句话说,访问类型声明对内存释放有影响吗?

虽然您示例中访问类型声明的 location 不影响内存的分配或释放方式,但 may do so. In either variation of procedure allocation, the execution of the enclosed block statement 按如下方式进行:

  • 声明部分的详细说明导致space被分配给访问类型的值;在这种情况下,该值在概念上可以被认为是指向 Integer 的指针或引用;该值的 space 通常分配在 execution stack.

  • 已处理的语句序列的执行导致对创建对象的allocator求值;在这种情况下,对象是一个 Integer,其值为 5;对象的 space 通常是从主机操作系统提供的内存池中分配的。

在任何一种情况下,当 块语句 结束时,分配给访问值的 space 被回收,但是分配给 space =11=] 值可能会保留。有关回收已分配存储的更多详细信息,请参阅 Ada Programming/Types/access

很有可能从中分配类型 T_Integer_Access 的对象的存储池(内存区域)永远不会被释放,因为您还没有定义自己的存储池; AARM 13.11 在 (2.a)

By default, the implementation might choose to have a single global storage pool, which is used (by default) by all access types, which might mean that storage is reclaimed automatically only upon partition completion. Alternatively, it might choose to create a new pool at each accessibility level, which might mean that storage is reclaimed for an access type when leaving the appropriate scope. Other schemes are possible.

--也就是说不是语言指定的

我认为在库级别之外定义对对象的访问类型是非常不寻常的。我从来没有做过,所以我不知道编译器实际上做了什么。

更新:

valgrind 在 macOS Sierra 上没有 运行,所以我尝试在 Debian jessie 上使用 GNAT GPL 2016;你的 Allocation 肯定会泄漏内存。

存储池是可终结的,因此您可以实现自己的;或者您可能会查看 Deepend.