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,1726119712bbda0d X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Interfacing to C non-void procedure with "out" parameters Date: 2000/01/17 Message-ID: #1/1 X-Deja-AN: 573811740 Content-transfer-encoding: 7bit References: <387f1c30.12554815@news.rdg.ac.uk> Content-Type: text/plain; charset="US-ASCII" X-ELN-Date: Mon Jan 17 08:58:55 2000 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.prod.itd.earthlink.net 948128335 38.26.192.110 (Mon, 17 Jan 2000 08:58:55 PST) Organization: EarthLink Network, Inc. Mime-version: 1.0 NNTP-Posting-Date: Mon, 17 Jan 2000 08:58:55 PST Newsgroups: comp.lang.ada Date: 2000-01-17T00:00:00+00:00 List-Id: In article <387f1c30.12554815@news.rdg.ac.uk> , S.J.Sangwine@Reading.ac.uk (Dr Steve Sangwine) wrote: > Can anyone shed light on the issue of interfacing between Ada95 and C > where there is a C procedure (non-void) which returns a result *and* > modifies some of its parameters? In neither Ada nor C can you directly modify a function parameter. In both languages you have to pass a pointer to the data, and modify the data designated by the pointer. Given this C header: int op (float *o); You can bind to it from Ada like this with Interfaces.C; use Interfaces.C; package P is type Float_Access is access all C_Float; pragma Convention (C, Float_Access); function Op (O : Float_Access) return Int; pragma Import (C, Op, "op"); end P; > The LRM, Appendix B doesn't say anything about this, and neither does > the Ada95 Rationale as far as I can see. There's nothing magic about the fact that the C procedure has a non-void return value. Just import the C procedure as an Ada function. > I have a C procedure, for which I wanted to write an Ada95 spec, and > import the C object code using pragma Import. Since the C procedure > has a return result, I need an Ada function (or do I?). Yes. Unless you want to throw away the procedure's return value, in which case you can bind to it using an Ada procedure. > An Ada > function must have parameters of mode 'in', implicitly or explicity, > yet the C procedure will modify one or more of its parameters. It's impossible for C to modify a parameter. Actually, I should qualify the above statement. In C, you can modify a parameter, for use as a local variable. However, any changes you make to the value of the parameter remain local, and are not transmitted back to the caller. The only way to modify a caller's value is the pass the address of the object to a C procedure. > To be > safe, I wrote a wrapper procedure in C which had an extra parameter, > and called this from Ada. The wrapper procedure called the real C > procedure and returned the return result of the real C procedure in > the extra parameter. That way the Ada spec can be a procedure and > everything is watertight, but it is a nuisance to have this extra, > trivial C wrapper procedure in between. It's a nuisance, and unnecessary. Get rid of it. > Would it work to allow the C procedure to modify a parameter that > the Ada code thinks is read-only (i.e. 'in'), or should I play safe as > described above. (By 'work', I mean work portably, not work when > compiled with a specific compiler.) You are free to modify a parameter on the C side that Ada thinks is in, just as can with a C caller. However, these changes are not propagated back to the Ada client, nor to a C client. > Since C programmers routinely confuse the concepts of procedure and > function (rant ....) this issue arises with almost any C code that one > wants to interface to Ada. There is no issue. -- You cannot think without abstractions; accordingly, it is of the utmost importance to be vigilant in critically revising your modes of abstraction. It is here that philosophy finds its niche as essential to the healthy progress of society. It is the critic of abstractions. Alfred North Whitehead