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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.2.137 with SMTP id 9mr15041237qaj.2.1372753984636; Tue, 02 Jul 2013 01:33:04 -0700 (PDT) X-Received: by 10.49.116.2 with SMTP id js2mr770090qeb.8.1372753984622; Tue, 02 Jul 2013 01:33:04 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!mx05.eternal-september.org!feeder.eternal-september.org!news.bbs-scene.org!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!y2no91147qax.0!news-out.google.com!f7ni507qai.0!nntp.google.com!y2no91143qax.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 2 Jul 2013 01:33:04 -0700 (PDT) In-Reply-To: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=193.205.78.232; posting-account=uPViEgkAAACC04vaTYL5Kyk76brV1MA_ NNTP-Posting-Host: 193.205.78.232 References: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <93fc7ff5-73b2-455a-85ae-3f3505144de2@googlegroups.com> Subject: Re: Thick bindings to a C library and gnattest: suggestions? From: Maurizio Tomasi Injection-Date: Tue, 02 Jul 2013 08:33:04 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: news.eternal-september.org comp.lang.ada:16034 Date: 2013-07-02T01:33:04-07:00 List-Id: 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.