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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,35ae3d13e899b684,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-21 12:06:04 PST Path: supernews.google.com!sn-xit-03!supernews.com!freenix!sunqbc.risq.qc.ca!news.maxwell.syr.edu!newsfeed.slurp.net!not-for-mail Newsgroups: comp.lang.ada Subject: Need help mapping a C struct to Ada Organization: Multimedia X-Newsreader: trn 4.0-test72 (19 April 1999) From: cts@kampong.aedinc.net ((null)) Message-ID: Date: Wed, 21 Mar 2001 19:56:38 GMT NNTP-Posting-Host: 208.4.231.188 X-Trace: newsfeed.slurp.net 985204598 208.4.231.188 (Wed, 21 Mar 2001 13:56:38 CDT) NNTP-Posting-Date: Wed, 21 Mar 2001 13:56:38 CDT Xref: supernews.google.com comp.lang.ada:5979 Date: 2001-03-21T19:56:38+00:00 List-Id: I'm trying to call some C functions from my Ada program. The C function prototype and data types are basically typedef struct { some fields... } element; typedef struct { int num_elements; element the_elements[0]; } element_table; void foo(size_t *table_size, element_table *table); My question is basically about the element_table structure. In C this means basically that element_table.the_elements doesn't have a fixed size. It is up to the programmer to figure out how much memory is needed for the desired number of elements and manual allocate the memory. My first try at using element_table in Ada95 looked something like type element is record some fields... end record; pragma Convention(C, element); type element_array is array (natural range <>) of element; pragma Convention(C, element_array); type element_table (count: positive) is record num_elements: integer; the_elements: element_array(0..count-1); end record; pragma Convention(C, element_table); procedure foo(table_size: access size_t; table: access element_table); pragma Import(C, foo, "foo"); That failed because Ada allocated space in element_table for the field 'count'. The C code, knowing nothing about 'count' proceded to put data that was supposed to go into the field 'the_elements' into the field 'num_elements'. My second attempt works with the compiler I'm using, but I don't think it is correct Ada in general. type element_array is array (natural range <>) of element; pragma Convention(C, element_array); type element_table (num_elements: positive) is record the_elements: element_array(0..count-1); end record; pragma Convention(C, element_table); In this case I'm just crossing my fingers and hoping that the Ada compiler lays out the element_table record the same way the C compiler does. What's the correct way of doing this? I think programming should be hopeless, i.e. I shouldn't ever say, "I _hope_ this works." My current solution isn't hopeless. Hopefully somebody can help me out here. -- ======================================================================= Life is short. | Craig Spannring Bike hard, ski fast. | cts@internetcds.com --------------------------------+------------------------------------