comp.lang.ada
 help / color / mirror / Atom feed
* C bindings, Interfaces.C.Pointers etc.
@ 2004-05-11 13:14 Dr. Adrian Wrigley
  2004-05-11 18:17 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Dr. Adrian Wrigley @ 2004-05-11 13:14 UTC (permalink / raw)


Hi folks!

I have a problem creating a binding to a C library.

The library makes extensive use of arrays of arrays of structs.
Both levels of arrays are of variable length.

 struct node { int index; double value; };
 struct problem { node **x; };

When I attempt to define Ada types for this type of thing,
I find I can't use access types to the arrays with the
necessary C convention.
"warning: this access type does not correspond to C pointer"
is the compiler error.

I have tried using Interfaces.C.Pointers, but I have run
into (soluble?) problems.

1)  How to I allocate an array (in Ada) and assign it to a
   C pointer? (in a single expression?)

2)  How do I free the allocaed array from the C pointer?
   Do I need to keep track of the return values from "new"
   separately from the pointers?

3)  How do I access the n'th element of the Array, given the
   C pointer.

4)  Is there some convenient shortcut to avoid using I.C.P?
   perhaps by declaring an Ada constrained array much larger than
   intended, and using a subset.
   Using I.C.P seems massively verbose (10x) for this application.

As I understand it, if the array is unconstrained, an access type
to the array contains the dope information, and can't be used
as a C pointer.

I have given an example of my problem code below (without I.C.P).

Any ideas?
--
Adrian Wrigley, Cambridge, England


procedure Problem is

-- C code is:
--
-- struct node
-- {
--      int index;
--      double value;
-- };

-- struct problem
-- {
--      node **x;
-- };

-- x is a pointer to an array of pointers to arrays of nodes (both variable size)

   type Node_T is record
      Index : Interfaces.C.Int;
      Value : Interfaces.C.Double;
   end record;
   pragma Convention (C, Node_T);

   type NodeArray_T is array (Interfaces.C.Size_T range <>) of Node_T;
   pragma Convention (C, NodeArray_T);
   type NodeArray_A is access all NodeArray_T;
   pragma Convention (C, NodeArray_A);

   type NodeArrayArray_T is array (Interfaces.C.Size_T range <>) of NodeArray_A;
   pragma Convention (C, NodeArrayArray_T);
   type NodeArrayArray_A is access all NodeArrayArray_T;
   pragma Convention (C, NodeArrayArray_A);

   type Problem_T is record
      X : NodeArrayArray_A;
   end record;
   pragma Convention (C, Problem_T);

   MyProblem : Problem_T;
   MyNode : Node_T;

begin

   MyProblem := (X => new NodeArrayArray_T (1 .. 100));

   for I in MyProblem.X'range loop
      MyProblem.X (I) := new NodeArray_T (1 .. I);
   end loop;

   MyNode := MyProblem.X (10)(5);

end Problem;




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

end of thread, other threads:[~2004-05-13 22:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-11 13:14 C bindings, Interfaces.C.Pointers etc Dr. Adrian Wrigley
2004-05-11 18:17 ` tmoran
2004-05-11 18:48 ` Jeffrey Carter
2004-05-12  4:50   ` Simon Wright
2004-05-13 15:26   ` Dr. Adrian Wrigley
2004-05-12  6:30 ` Martin Krischik
2004-05-13 15:56   ` Dr. Adrian Wrigley
2004-05-13 17:37     ` Martin Krischik
2004-05-13 22:42     ` Jeffrey Carter

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