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 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-21 13:28:53 PST Path: supernews.google.com!sn-xit-02!supernews.com!news.tele.dk!204.94.211.44!enews.sgi.com!newshub2.rdc1.sfba.home.com!news.home.com!news1.frmt1.sfba.home.com.POSTED!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Need help mapping a C struct to Ada References: X-Newsreader: Tom's custom newsreader Message-ID: Date: Wed, 21 Mar 2001 21:25:40 GMT NNTP-Posting-Host: 24.20.190.201 X-Complaints-To: abuse@home.net X-Trace: news1.frmt1.sfba.home.com 985209940 24.20.190.201 (Wed, 21 Mar 2001 13:25:40 PST) NNTP-Posting-Date: Wed, 21 Mar 2001 13:25:40 PST Organization: Excite@Home - The Leader in Broadband http://home.com/faster Xref: supernews.google.com comp.lang.ada:5980 Date: 2001-03-21T21:25:40+00:00 List-Id: > type element_table (count: positive) is record > num_elements: integer; > the_elements: element_array(0..count-1); > end record; >That failed because Ada allocated space in element_table for the >field 'count'. The C code, knowing nothing about 'count' proceded An Ada description of the C data structure is type element_table (count: Interfaces.C.Int) is record the_elements: element_array(Interfaces.C.Unsigned); end record; There is a single "count", and the number of elements is potentially very large. If C routines allocate these objects, they will pass back pointers, not massive structures, so there's no worry about space, but it's up to you to do the subscript range checking. If you actually need to allocate one of these things in Ada, clearly the "element_array(Interfaces.C.Unsigned)" is unreasonable, so you will have to figure out some maximum on the number of elements and do Max_Possible_Index:constant Interfaces.C.Int := ??? type element_table (count: Interfaces.C.Int) is record the_elements: element_array(0 .. Max_Possible_Index); end record; >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. A very last resort. Think "when the C compiler sees one of these objects being used, what information does it know?". It knows the first part of storage is an int, followed by any number of "element"s. If the Ada compiler knows more, such as a value for "num_elements" that could differ from "count", or a dynamic upper bound for the "element_array", clearly the Ada compiler will have to store that additional information someplace, and thus the storage arrangements must necessarily differ. You must inform the Ada compiler of just what (how little) you know when you get one of these things from a C call. You should also use a representation clause for element_table use record count at 0 range 0 .. Interfaces.C.Int'size - 1; the_elements at Interfaces.C.Int'size/System.Storage_Unit range 0 .. (Max_Possible_Index+1)*Interfaces.C.Int'size - 1; end record; That will tell the compiler what you want, tell future program maintainers what you want, and let the compiler tell you if what it's going to do differs from what you want.