From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7c885a90814b7f72 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Array resizing Date: 1997/10/19 Message-ID: #1/1 X-Deja-AN: 282533948 References: <01bcdc8c$924d4180$e7207cc1@anishome> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-10-19T00:00:00+00:00 List-Id: In article <01bcdc8c$924d4180$e7207cc1@anishome>, "Dmitriy Anisimkov" wrote: >Can I resize dinamicaly allocatad array >or I mast allocate new array and copy previous to it? > >If array is big copying is more slowely than reallocation. Once an array is allocated, its size is fixed forever. However, if you know the maximum size that you'd ever need it to be, you could declare a mutable type that can have different sizes during its lifetime. type Item_Array is array (Positive range <>) of Item; subtype Length_Range is 0 .. Max_Size; type Mutable_Item_Array (Length : Length_Range := 0) is record Items : Item_Array (1 .. Length); end record; Item_Buffer : Mutable_Item_Array; procedure Update (Items : Item_Array) is begin Item_Buffer := (Items'Length, Items); end; So during its lifetime, the Item_Buffer contains an array whose length changes. If this isn't what you want (perhaps you don't know the maximum size), then yes, you would have to allocate another array, and copy the old items and new items to the new array. It may be that you really want some other data structure, at a higher level of abstraction, implemented in terms of an array. One idea is to allocate array chunks, linked together as a list. When you need more room, you just allocate another, fixed-size chunk and attach it to the end of the list. You won't be able to directly access a specific item though; you'll have to figure which node of the list it's on, then iterate though the list until you find that node. Another similar idea is to use a B-tree. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271