开发者

Dynamic array allocation of a record in Ada

开发者 https://www.devze.com 2023-01-27 05:21 出处:网络
I am trying to dynamically allocate a large array in Ada (well, an array of an array). For instance, I\'m able to dynamically allocate an object like so:

I am trying to dynamically allocate a large array in Ada (well, an array of an array). For instance, I'm able to dynamically allocate an object like so:

type Object;
type ObjPtr is access Object;
OP : ObjPtr;
-- sometime later
OP := new Object;
OP.Index := I;--OP.Ptr.all;
Free(OP);

I'm trying to emulate this benchmark code:

Object **objList = new Object*[500000];
int32_t *iList = new int32_t[500000];
for (int32_t i = 0; i < 500000; ++i)
{
  objList[i] = new Object;
  iList[i] = Object::getIndex(objList[i]);
  delete objList[i];
}
delete[] iList;
delete[] objList;

Sadly, I'm unable to even do something like this c++ equivalent:

Object *objList = new Object*[500000];

I came up with this 开发者_StackOverflowmuch so far:

type objs is array (Positive range <>) of Object;
type objList is access objs;

But I'm probably way off.


In Ada your C++ code would translate roughly to the following:

Alloc_Count : constant := 500_000;
type ObjPtr is access Object;
type ObjArray is array (1 .. Alloc_Count) of ObjPtr;
OA : ObjArray;
begin
   for I in OA'Range loop
   OA(I) := new Object;
   -- ... do the other things
end loop;

If you want to use dispatching operations with your objects (i.e. the Object is defined as a tagged type), use Object'class instead of Object in the ObjPtr declaration.

0

精彩评论

暂无评论...
验证码 换一张
取 消