From: cts@kampong.aedinc.net ((null))
Subject: Need help mapping a C struct to Ada
Date: Wed, 21 Mar 2001 19:56:38 GMT
Date: 2001-03-21T19:56:38+00:00 [thread overview]
Message-ID: <WH7u6.40$Je5.495@newsfeed.slurp.net> (raw)
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
--------------------------------+------------------------------------
next reply other threads:[~2001-03-21 19:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-03-21 19:56 (null) [this message]
2001-03-21 21:25 ` Need help mapping a C struct to Ada tmoran
2001-03-22 20:53 ` tmoran
2001-03-21 22:21 ` Jeffrey Carter
2001-03-21 23:12 ` Florian Weimer
2001-03-30 5:59 ` David Thompson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox