comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison <dennison@telepath.com>
Subject: Re: Question about interfacing C and Ada95
Date: 1999/11/09
Date: 1999-11-09T00:00:00+00:00	[thread overview]
Message-ID: <809dv6$a7$1@nnrp1.deja.com> (raw)
In-Reply-To: 808la7$4b9$1@trog.dera.gov.uk

In article <808la7$4b9$1@trog.dera.gov.uk>,
  "Hovers" <hovers@dera.gov.uk> wrote:
> In followup would this be sort of correct.
>
> It falls over however on the first text_io.put_line

If your compiler follows the implementation advice, that's exactly what
I'd expect to happen. See below.

> void createObject ( void **inst, int *error_code )

So the C side thinks its getting a pointer to an integer for error_code.

>    type Access_Int_Type is access Interfaces.C.Int;
>    Error_Code : Access_Int_Type := NULL;
>    procedure Create_Object
>       ( Instance : in out Instance_Type;
>         Error_Code : in out Access_Int_Type );
>        pragma Import ( C, Create_Object, "createObject" );

If your compiler follows the implmentation advice in the RM, "in out
Access_Int_Type" will be roughly equivalent to a C "*Access_Int_Type".
In other words, what C will be getting is a *pointer* to the Error_Code
object you pass in on the Ada side (which is itself a pointer). So in
effect C is looking for a *int, but you will be giving it a **int.

>    Create_Object ( Instance, Error_Code );

So here you pass in Error_Code, which Ada passes to C as a pointer to
Error_Code (which itself is a null pointer), C dereferences your
pointer, and overrides the NULL there with an error return code (let's
assume the value is 1).

>    Text_IO.Put_Line ( Interfaces.C.Int'image ( Error_Code.all ) );

Error_Code's value is now 1. That most likely is not a valid address for
a pointer in your system. Now you try to dereference it and print the
value of the integer at memory location 1. *BOOM*!

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Before you buy.




  parent reply	other threads:[~1999-11-09  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-08  0:00 Question about interfacing C and Ada95 Hovers
1999-11-08  0:00 ` tmoran
1999-11-09  0:00   ` Robert Dewar
1999-11-08  0:00 ` Robert Dewar
1999-11-09  0:00   ` Hovers
1999-11-09  0:00     ` Matthew Heaney
1999-11-09  0:00     ` Ted Dennison [this message]
1999-11-12  0:00       ` Hovers
1999-11-12  0:00         ` Robert Dewar
1999-11-12  0:00           ` Ted Dennison
1999-11-13  0:00             ` Robert Dewar
1999-11-08  0:00 ` Matthew Heaney
replies disabled

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