comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@nospam.acm.org>
Subject: Re: Allocation questions
Date: Thu, 28 May 2009 16:59:40 GMT
Date: 2009-05-28T16:59:40+00:00	[thread overview]
Message-ID: <0qzTl.121304$DP1.112499@attbi_s22> (raw)
In-Reply-To: <4a1e5d9e$0$2868$ba620e4c@news.skynet.be>

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



  parent reply	other threads:[~2009-05-28 16:59 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28  9:46 Allocation questions Olivier Scalbert
2009-05-28 10:02 ` Martin
2009-05-28 11:55   ` xavier grave
2009-05-28 12:19     ` Ludovic Brenta
2009-05-28 13:00       ` Georg Bauhaus
2009-05-28 23:13       ` Robert A Duff
2009-05-28 10:05 ` Dmitry A. Kazakov
2009-05-28 10:40   ` Martin
2009-05-28 12:32     ` Olivier Scalbert
2009-05-28 13:39       ` Martin
2009-05-28 13:57         ` Olivier Scalbert
2009-05-28 14:51         ` Adam Beneschan
2009-05-28 17:58           ` Randy Brukardt
2009-05-28 14:04       ` Dmitry A. Kazakov
2009-05-28 16:59 ` Jeffrey R. Carter [this message]
2009-05-28 17:26   ` Olivier Scalbert
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox