comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Re: calling an ada procedure from C++
Date: Mon, 17 Nov 2003 18:28:40 GMT
Date: 2003-11-17T18:28:40+00:00	[thread overview]
Message-ID: <sr8ub.46021$bQ3.24980@nwrdny03.gnilink.net> (raw)
In-Reply-To: 1069079554.9445@master.nyc.kbcfp.com


"Hyman Rosen" <hyrosen@mail.com> 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.





  reply	other threads:[~2003-11-17 18:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-12 15:11 calling an ada procedure from C++ Anthony Moss
2003-11-17  4:04 ` red floyd
2003-11-17  7:37 ` tmoran
2003-11-17 10:38 ` Duncan Sands
2003-11-17 14:32   ` Hyman Rosen
2003-11-17 18:28     ` Frank J. Lhota [this message]
2003-11-18  0:03       ` Ludovic Brenta
2003-11-18  0:32         ` Frank J. Lhota
2003-11-22  5:25           ` Dave Thompson
2003-11-17 21:13 ` sk
replies disabled

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