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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f0b32efa497bedb7,start X-Google-Attributes: gid103376,public From: Richard Beare Subject: Accessing C arrays Date: 1998/08/11 Message-ID: <35CFEF57.DEC91044@cmis.csiro.au>#1/1 X-Deja-AN: 379984885 Content-Transfer-Encoding: 7bit Sender: news@news.nsw.CSIRO.AU X-Nntp-Posting-Host: bondi.nsw.cmis.csiro.au Content-Type: text/plain; charset=us-ascii Organization: CSIRO CMIS Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-08-11T00:00:00+00:00 List-Id: I'm trying to build some bindings to some of our in house C code, and one thing I would like to be able to do in a nice way is to put the c allocated array memory into an ada form, complete with ranges. I run into problems when trying to write a function that returns an access type referencing the memory block allocated in the C code. Here's what I had in mind --------------------------------------- with System; generic type Pixel is private; package Image_Types is type Image_2d is array (Natural range <>, Natural range <>) of aliased Pixel; type Image_1d is array (Natural range <>) of aliased Pixel; type H_Image_2d is access all Image_2d; type H_Image_1d is access all Image_1d; function Import(P : System.Address; X, Y : Natural) return Image_2d; function Import(P : System.Address; Len : Natural) return Image_1d; function Import(P : System.Address; X, Y : Natural) return H_Image_2d; end Image_Types; --------------------------------------- package body Image_Types is function Import(P : System.Address; X, Y : Natural) return Image_2d is Ada_Version : Image_2d(1 .. X, 1 .. Y); for Ada_Version'Address use P; begin return Ada_Version; end; ------------------------------------------------------------------ function Import(P : System.Address; Len : Natural) return Image_1d is Ada_Version : aliased Image_1d(1 .. Len); for Ada_Version'Address use P; begin return Ada_Version; end; ------------------------------------------------------------------- function Import(P : System.Address; X, Y : Natural) return H_Image_2d is L : H_Image_2d; Ada_Version : aliased Image_2d(1 .. X, 1 .. Y); for Ada_Version'Address use P; begin L := Ada_Version'Access; return L; end; end Image_Types; ---------------------------------------------------- I'm not entirely sure what is wrong with the last function, but it doesn't compile with gnat. The message is L := Ada_Version'Access; | >>> object subtype must statically match designated subtype I'm not sure that this is the appropriate approach anyway. Is there a way to create the additional information required by ada without overwriting the work done by the C code. Surely something like l : h_image_2d := new image_2d(1 .. X, 1 .. Y); for l.all'address use p; is performing the allocation anyway, and overwriting the original data. (This doesn't compile either - I'm not sure about the correct use of representation clauses.) Any advice welcome. -- Richard Beare Richard.Beare@cmis.csiro.au