comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Thick bindings to a C library and gnattest: suggestions?
Date: Mon, 01 Jul 2013 10:16:05 -0700
Date: 2013-07-01T10:16:05-07:00	[thread overview]
Message-ID: <kqsd6d$1ik$2@dont-email.me> (raw)
In-Reply-To: <bbb47fc9-3b53-47a8-b996-3a495e48445e@googlegroups.com>

On 07/01/2013 02:02 AM, ziotom78@gmail.com wrote:
>
> First question: the vectors used by the CFITSIO library are sometimes
> huge (millions of elements), sometimes very small (~ 10 elements).
> I decided to always allocate them on the heap, using declarations like
> these:
>
>     subtype Double is Interfaces.C.double;
>     type Double_Array is array (Positive range <>)
>       of Interfaces.C.double;
>     pragma Convention (C, Double_Array);
>     type Double_Array_Ptr is access Double_Array;
>
> (similar declarations exist for arrays of integers/long...), and then
> using "new Double_Array" whenever I need to allocate room for an
> array. Every binding function only uses Double_Array_Ptr. Is this the best
> way to do this in Ada? Every text I read about Ada says to stay away
> from objects allocated on the heap, but I do not see any other
> solution here.

One should not use heap allocation unnecessarily, as it requires memory 
management, which is often complex and error prone, but that does not mean one 
should never use heap allocation. When it is necessary, there is no alternative. 
What one should not do is have visible access types in public package specs.

As others have pointed out, generally the application is what knows the sizes of 
the objects it works on, and that decides whether heap allocation is required.

I would not use "_Array" in type names. We know it's an array because the type 
declaration says so. The type name should indicate its intended meaning or use, 
not its implementation.

Another approach not mentioned is the use of Ada.Containers.Vectors. This 
implements unbounded arrays. Internally, the arrays are allocated on the heap 
and memory managed, but this complication is hidden from the user. Ada 12 
provides the ability to use them like an array (use of those features currently 
limits you to a single compiler, though).

(Aside: in a world in which GB of RAM are common, it seems odd for a language to 
require explicit heap allocation, and the associated memory management, for 
objects that fit in memory, but not in the stack. Surely it would be better for 
the language to allow the developer to simply declare the object, and for the 
compiler to decide where it will fit, and allocate it and manage its memory 
appropriately. A means to disable automatic heap allocation would be needed for 
systems that disallow heap allocation.)

-- 
Jeff Carter
"Hold your temper. Count ten.... Now let 'er go.
You got a good aim."
Never Give a Sucker an Even Break
105


  parent reply	other threads:[~2013-07-01 17:16 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-01  9:02 Thick bindings to a C library and gnattest: suggestions? ziotom78
2013-07-01  9:45 ` Dmitry A. Kazakov
2013-07-01 11:11   ` Maurizio Tomasi
2013-07-01 11:41     ` Simon Wright
2013-07-01 12:00       ` Maurizio Tomasi
2013-07-01 12:42         ` Dmitry A. Kazakov
2013-07-01 19:07           ` Simon Wright
2013-07-01 12:32     ` Dmitry A. Kazakov
2013-07-01 12:41       ` Maurizio Tomasi
2013-07-01 12:47       ` Simon Wright
2013-07-02  8:55     ` Georg Bauhaus
2013-07-02  8:33   ` Maurizio Tomasi
2013-07-02  8:58     ` Dmitry A. Kazakov
2013-07-02 16:58     ` Robert A Duff
2013-07-02 17:00     ` Jeffrey Carter
2013-07-01 17:16 ` Jeffrey Carter [this message]
2013-07-02  4:24   ` Randy Brukardt
2013-07-02  4:37     ` Shark8
2013-07-02  5:04     ` tmoran
2013-07-02 22:27       ` Randy Brukardt
2013-07-03 12:02   ` Jacob Sparre Andersen
2013-07-02  3:16 ` Jerry
2013-07-02  4:02   ` Shark8
replies disabled

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