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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,782d14fd472db944 X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: Question about interfacing C and Ada95 Date: 1999/11/09 Message-ID: <809dv6$a7$1@nnrp1.deja.com>#1/1 X-Deja-AN: 546448748 References: <806ud0$lu1$1@trog.dera.gov.uk> <8076h5$d3c$1@nnrp1.deja.com> <808la7$4b9$1@trog.dera.gov.uk> X-Http-Proxy: 1.0 x21.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Tue Nov 09 15:18:01 1999 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.6 [en] (WinNT; I) Date: 1999-11-09T00:00:00+00:00 List-Id: In article <808la7$4b9$1@trog.dera.gov.uk>, "Hovers" 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.