Amben Uchiha
Amben Uchiha

Reputation: 463

General Access type Ada

I am still confused on how all keyword works in a general access type

what's the difference between:

type int_access is access all Integer; to type int_access is access Integer;

for example:

type int_ptr is access all Integer;

Var : aliased Integer := 1;

Ptr : int_ptr := Var'Access;

the code works fine but if I remove the all keyword it gives an error that result must be general access type and I must add all.

Upvotes: 8

Views: 758

Answers (2)

Shark8
Shark8

Reputation: 4198

Explained here: Memory Management with Ada 2012.

Upvotes: 2

Niklas Holsti
Niklas Holsti

Reputation: 2142

Pool-specific access types -- those without the "all" -- can be used only for objects allocated in the heap (or in some user-defined storage pool) with the "new" keyword.

So this is OK:

type Int_Ptr is access Integer;
Prt: Int_Ptr := new Integer;

General access types -- those with the "all" -- can be used both for heap-allocated objects, and for any other object that is marked "aliased". So this is also OK:

type Int_Ptr is access all Integer;
Prt: Int_Ptr := new Integer;

So the rules, in brief, are:

  • without "all": only objects allocated with "new"
  • with "all": in addition, any object marked "aliased".

Upvotes: 9

Related Questions