comp.lang.ada
 help / color / mirror / Atom feed
From: Maurizio Tomasi <ziotom78@gmail.com>
Subject: Re: Thick bindings to a C library and gnattest: suggestions?
Date: Tue, 2 Jul 2013 01:33:04 -0700 (PDT)
Date: 2013-07-02T01:33:04-07:00	[thread overview]
Message-ID: <93fc7ff5-73b2-455a-85ae-3f3505144de2@googlegroups.com> (raw)
In-Reply-To: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net>

On Monday, July 1, 2013 11:45:04 AM UTC+2, Dmitry A. Kazakov wrote:
> Why don't you simply pass the array down to the C subprogram? You can do
> something like:
> 
>    type Double_Array is array (Positive range <>)
>       of aliased Interfaces.C.double;
>    pragma Convention (C, Double_Array);
>    procedure Foo (A : Double_Array);
> 
> Implementation:
> 
>    type Double_Ptr is access all Interfaces.C.double;
>    pragma Convention (C, Double_Ptr);
> 
>    procedure Foo (A : Double_Array) is
>    --
>    -- Assuming foo's signature in C:
>    --
>    --    foo (double * a, unsigned n);
>    --
>       procedure Internal (A : Double_Ptr; N : Interfaces.C.unsigned);
>       pragma Import (C, Internal, "foo");
>    begin
>       Internal (A (A'First)'Access, A'Length);
>    end Foo;

Hi Dmitry,

  I like a lot this solution, but apparently I am not able even to implement this correctly. I wrote a dummy package to make my problem clearer:

with Interfaces.C;

procedure Test_Package is
   
   type Vector is array (Positive range <>) of aliased Natural;
   
   type Pointer is access all Natural;
   pragma Convention (C, Pointer); 
   
   type Const_Pointer is access constant Natural;
   pragma Convention (C, Const_Pointer); 

   procedure Read_Vector_From_File (A : out Vector) is 
      
      procedure Internal (A : Pointer; N : Interfaces.C.unsigned); 
      pragma Import (C, Internal, "read_vector"); 
      
   begin 
      Internal (A (A'First)'Access, A'Length); 
   end Read_Vector_From_File; 
   
   procedure Write_Vector_To_File (A : Vector) is 
      
      procedure Internal (A : Const_Pointer; N : Interfaces.C.unsigned); 
      pragma Import (C, Internal, "write_vector"); 
      
   begin 
      Internal (A (A'First)'Access, A'Length); 
   end Write_Vector_To_File; 
   
   A : Vector (1 .. 3);
   
begin
   Read_Vector_From_File (A);
   Write_Vector_To_File (A);
end Test_Package;

When I compile this program, both occurrences of "A (A'First)'Access" in Read_Vector and Write_Vector make GNAT complain:

test_package.adb:19:17: non-local pointer cannot point to local object
test_package.adb:28:17: non-local pointer cannot point to local object

I do not understand what causes this problem: in both cases the address of A's first element is not stored in a global variable but used as the argument to a procedure call. So how can this be a "non-local pointer"?

Maurizio.

  parent reply	other threads:[~2013-07-02  8:33 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 [this message]
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
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