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,7f2513845b4ef39f X-Google-Attributes: gid103376,public From: tmoran@bix.com Subject: Re: More C Date: 2000/01/28 Message-ID: #1/1 X-Deja-AN: 578879025 References: <86skkc$9281@news.cis.okstate.edu> X-Complaints-To: abuse@pacbell.net X-Trace: nnrp3-w.snfc21.pbi.net 949088400 206.170.24.46 (Fri, 28 Jan 2000 11:40:00 PST) Organization: SBC Internet Services NNTP-Posting-Date: Fri, 28 Jan 2000 11:40:00 PST Newsgroups: comp.lang.ada Date: 2000-01-28T00:00:00+00:00 List-Id: >In memory (assuming 8 bit pointers) we have > >num_chars_in_b >| pointer array >| | | >03 20 ...... 01 10 10 23 58 20 48 . . . > >The only place in that memory where it tells you the length of the >array is num_chars_in_b. No matter how you translate that structure >into Ada, Ada can't give you the length of the array independent of >num_chars_in_b, because it doesn't have the information. Or so >I understand it. Quite so. But in the message I posted it said: >I presume you really want Colors to point to a Color_List, not to a >single color. Note that Colors will point to a list of N colors, >where N may, or may not, equal Number_Of_Colors. in which case the pointer points to an array and the compiler either knows its bounds or knows where to look to find its bounds (if they are dynamic). The case you have is: >If you really must match the memory layout of the C SDL_Palette, > >type Arbitrary_Color_List is array(C.Int range) of aliased SDL_Color; >type Arbitrary_Color_List_Access is access all Color_List; > >type SDL_Palette is record > Number_Of_Colors: C.Int; > Colors: Arbitrary_Color_List_Access; >end record; > >is probably your best bet. You then have to do your own index >checking, just as in C, and you'll want a representation clause. Since the compiler knows the (unchanging) bounds, here all of C.Int, it needn't put them in memory anywhere and can set the pointer to the array Colors to be the identical bits to a pointer to the first element of Colors, which matches the C layout. There's no guarantee it will do this, so you must experiment to see what happens and put big signs around warning of danger, but it will probably work. An alternative, more work but likely more safely portable, is the suggestion from Jeff Carter of >type SDL_Palette is record > Num_Colors : C.Int; > Address_Of_Color_Array : System.Address; >end record; > >Given: > > Palette : SDL_Palette; > >containing data from C, use > >type Color_Set is array (C.Int range <>) of SDL_Color; > >Colors : Color_Set (1 .. Palette.Num_Colors); >for Colors'Address use Palette.Address_Of_Color_Array; Note, however, that a C pointer is not necessarily the same as an Ada System.Address (consider various i86 memory models) so that probably ought to be modified to type SDL_Palette is record Num_Colors : C.Int; C_Access_Of_Color_Array : Interfaces.C.Strings.char_array_access; end record; and then use System.Address_To_Access_Conversions to convert C_Access_Of_Color_Array to a System.Address to use in the for Colors'Address use the_address_from_C_Access_Of_Color_Array; PS. I should have said type Arbitrary_Color_List is array(C.Int range 0 .. C.Int'last) of aliased SDL_Color; since you probably want index 0 (or maybe 1), rather than C.Int'first, to indicate the first element.