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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns14feed!worldnet.att.net!attbi_s22.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Allocation questions References: <4a1e5d9e$0$2868$ba620e4c@news.skynet.be> In-Reply-To: <4a1e5d9e$0$2868$ba620e4c@news.skynet.be> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <0qzTl.121304$DP1.112499@attbi_s22> NNTP-Posting-Host: 173.16.158.68 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s22 1243529980 173.16.158.68 (Thu, 28 May 2009 16:59:40 GMT) NNTP-Posting-Date: Thu, 28 May 2009 16:59:40 GMT Organization: AT&T ASP.att.net Date: Thu, 28 May 2009 16:59:40 GMT Xref: g2news2.google.com comp.lang.ada:6079 Date: 2009-05-28T16:59:40+00:00 List-Id: Olivier Scalbert wrote: > > Here is the code (As delay is a reserved word in Ada, I use Delay_T as I > found nothing better !): Delay_Info Delay_Data > 1) Is there a way to avoid the dynamic array allocation, as I do not > know the Time_Value at compile time ? You cannot avoid dynamic allocation. However, you can do the dynamic allocation on the stack (variable-sized record), or on the heap (explicit access values or an unbounded data structure such as Ada.Containers.Vectors). An object allocated on the stack is likely to have a smaller maximum size than one allocated on the heap. If you are going to use the heap, I would recommend using a data structure, as it will do the memory management for you. The memory management in Ada.Containers is more likely to be correct than something you write yourself, and to remain correct as you modify your code. If you are going to use a variable-sized record, you should declare (sub)types that reflect the constraints implicit in the implementation. For example, there is a maximum usable value of type Time, since you have to be able to multiply any supplied value by the sampling rate. This also implies a maximum number of samples that may be handled at a time: type Time_Base is new Float; Sampling_Rate : constant := 44_100; Max_Time : constant Time_Base := Time_Base'Last / Sampling_Rate; subtype Time is Time_Base range 0.0 .. Max_Time; Max_Samples : constant Positive := Integer (Time'Truncation (Max_Time * Sampling_Rate) ); As this last declaration is likely to raise Constraint_Error, it's probably better to start with the maximum number of samples and work backward: Max_Samples : constant := 30_000; -- Arbitrary value; replace with something meaningful from the application. Max_Time : constant Time_Base := Time_Base (Max_Samples) / Sampling_Rate; subtype Sample_Index is Positive range 1 .. Max_Samples; type Sample_List is array (Sample_Index range <>) of Sample; package Sample_Lists is new Ada.Containers.Vectors (Index_Type => Sample_Index, Element_Type => Sample); -- Jeff Carter "What lazy lout left these wires all over the lawn?" Poppy 98