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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,d4984245154c8ef1 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!newsfeed2.dallas1.level3.net!news.level3.com!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Interfacing C type, unconstrained array with record Date: Sat, 16 Oct 2010 17:41:40 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1287265283 16641 192.74.137.71 (16 Oct 2010 21:41:23 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sat, 16 Oct 2010 21:41:23 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:yk6PMu8qzDDkFm0+8TZe07qvj6A= Xref: g2news2.google.com comp.lang.ada:15546 Date: 2010-10-16T17:41:40-04:00 List-Id: "Dmitry A. Kazakov" writes: > You need a flat array, e.g. > > with Interfaces; use Interfaces; > with Interfaces.C; use Interfaces.C; I'd probably write: with Interfaces.C; use Interfaces; and then refer to C.int instead of Int. No big deal. > type SDL_Color is record > R, G, B, Unused : Unsigned_8; > end record; > pragma Convention (C, SDL_Color); > type SDL_Color_Array is array (Positive) of aliased SDL_Color; I think you want this indexed by the same type as N_Colors. And why does it need to have aliased components? So perhaps: type SDL_Color_Array is array (Int range 1..1_000_000) of SDL_Color; > pragma Convention (C, SDL_Color_Array); > type SDL_Palette is record > N_Colors : Int; > Colors : SDL_Color_Array; > end record; > pragma Convention (C, SDL_Palette); > > Now, you cannot directly declare a SDL_Palette variable, because it is too > large. You should do it as you would do in C, i.e. allocate some amount of > memory and then set structure pointer to it. You do not need 'new' or > access types in Ada. It can be on the stack like this: > > with System.Storage_Elements; use System.Storage_Elements; > ... > Raw_Memory : Storage_Array (1..1_024); -- Memory > Palette : SDL_Palette; > for Palette'Address use Raw_Memory'Address; I think you need to worry about alignment here. Perhaps: pragma Assert(SDL_Palette'Alignment = C.int'Alignment); for Raw_Memory'Alignment use C.int'Alignment; > begin > Palette.N_Colors := 20; > > Of course computing memory size from N_Colors is up to you (as in C). You might want to say: Colors: SDL_Color_Array renames Palette.Colors(1..Palette.N_Colors); and then refer to components of Colors, so you get array bounds checking. - Bob