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-Thread: 103376,8ecbc35ea893182f X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Interfacing to C: big structures Date: Tue, 26 Feb 2008 10:33:15 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <62idb0F23ddfnU1@mid.individual.net> <62ip0mF23cobbU1@mid.individual.net> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1204039996 732 192.74.137.71 (26 Feb 2008 15:33:16 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Tue, 26 Feb 2008 15:33:16 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:rXOuMxMxK8pQtq90Ck7q2Fu/QPg= Xref: g2news1.google.com comp.lang.ada:20094 Date: 2008-02-26T10:33:15-05:00 List-Id: "Alex R. Mosteo" writes: > Robert A Duff wrote: >> "Alex R. Mosteo" writes: >> >>> 2. Create an Ada type as >>> >>> type S is new Ada.Streams.Stream_Element_Array >>> (1 .. Stream_Offset (Size_Of_S)); >>> pragma Pack (S); -- Not sure if this is even needed. >>> >>> Note that this type gets its size at elaboration time, and thus this is >>> what fixes this type if the C corresponding struct changes. >> >> Nice trick. >> >> But I think you need to make sure the alignment is correct. > > I'm out of my expertise here, but why it's that necessary? In the end > you're passing S address to the C subprograms and I don't see how > alignment may have influence (but, again, I don't know about this). Suppose the C struct contains a double, and suppose the C compiler therefore insists that all objects be allocated on an 8-byte boundary. On some machines, failure to do so will cause the program to crash; on others, it will cause the program to be slow. But the Ada compiler thinks it's an array of bytes, so S'Alignment will probably be 1, so the compiler can allocate objects at an improperly-aligned address. type T is record Flag : Boolean; X : S; end record; - Bob