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=-0.4 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00 autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2c25d9643f05ffd1 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-17 10:28:40 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!c03.atl99!chi1.webusenet.com!news.webusenet.com!cyclone1.gnilink.net!spamkiller2.gnilink.net!nwrdny03.gnilink.net.POSTED!53ab2750!not-for-mail From: "Frank J. Lhota" Newsgroups: comp.lang.ada References: <3fb24ce4$1@baen1673807.greenlnk.net> <1069079554.9445@master.nyc.kbcfp.com> Subject: Re: calling an ada procedure from C++ X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: Date: Mon, 17 Nov 2003 18:28:40 GMT NNTP-Posting-Host: 151.203.220.105 X-Complaints-To: abuse@verizon.net X-Trace: nwrdny03.gnilink.net 1069093720 151.203.220.105 (Mon, 17 Nov 2003 13:28:40 EST) NNTP-Posting-Date: Mon, 17 Nov 2003 13:28:40 EST Xref: archiver1.google.com comp.lang.ada:2581 Date: 2003-11-17T18:28:40+00:00 List-Id: "Hyman Rosen" wrote in message news:1069079554.9445@master.nyc.kbcfp.com... > Duncan Sands wrote: > > Standard C does not permit returning structures on the stack IIRC. > > C has permitted this for some decades now. > But you must make sure that the C and Ada compilers > agree on how this is done. The original, vanilla K&R C book said: "This implies that structures may not be assigned to or copied as a unit, and that they can not be passed to or returned from functions. (These restrictions will be removed in forthcoming versions.)" IIRC said forthcoming versions were out there shortly afterwards. The problem, as Hyman Rosen correctly pointed out, is the mechanism by which the structure is returned. I know of at least mechanisms used to return a C structure from a function: 1. The function pushes return structure onto the stack, and the caller pops the return value. 2. The function has an additional, implicit parameter that specifies the address of the return structure. The caller creates a buffer for this return value, and passes a pointer to this buffer as the additional parameter. The function then writes directly to this buffer. 3. The function has an implicit static structure variable for the return value. It fills in this static area with the return value, and returns the address of this static area to the caller. Hopefully, mechanism 3 is not being used by any current C compiler. This method creates both reentrancy and multithreading problems. Some compilers use either mechanism 1 or mechanism 2, depending on the nature of the structure. Some use the rule of thumb that (1) is used if the structure is small and (2) is used if the structure is large enough. In order to be sure of how a particular C compiler returns a given structure type, I would recommend doing an assembly listing for the current C code. Also, be forewarned of the portability issue that different C compilers will handle this in different ways.