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,b16d5240727960c4,start X-Google-Attributes: gid103376,public Path: controlnews3.google.com!news1.google.com!news.glorb.com!newsrout1.ntli.net!news-in.ntli.net!newspeer1-win.server.ntli.net!newsfe1-win.POSTED!53ab2750!not-for-mail From: "Dr. Adrian Wrigley" Subject: C bindings, Interfaces.C.Pointers etc. User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Message-Id: Newsgroups: comp.lang.ada MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Date: Tue, 11 May 2004 14:14:30 +0100 NNTP-Posting-Host: 81.100.88.147 X-Complaints-To: http://www.ntlworld.com/netreport X-Trace: newsfe1-win 1084281223 81.100.88.147 (Tue, 11 May 2004 13:13:43 GMT) NNTP-Posting-Date: Tue, 11 May 2004 13:13:43 GMT Organization: ntl Cablemodem News Service Xref: controlnews3.google.com comp.lang.ada:442 Date: 2004-05-11T14:14:30+01:00 List-Id: 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;