comp.lang.ada
 help / color / mirror / Atom feed
* Need help mapping a C struct to Ada
@ 2001-03-21 19:56 (null)
  2001-03-21 21:25 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: (null) @ 2001-03-21 19:56 UTC (permalink / 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
 --------------------------------+------------------------------------



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-03-30  5:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-21 19:56 Need help mapping a C struct to Ada (null)
2001-03-21 21:25 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox