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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ac6f6c30c45f808a X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!news-out2.kabelfoon.nl!83.128.0.10.MISMATCH!newsfeed.kabelfoon.nl!213.218.66.70.MISMATCH!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!feeder2.news.jippii.net!feeder1.news.jippii.net!reader1.news.jippii.net!53ab2750!not-for-mail Message-ID: <41EBDAD5.4000703@nowhere.fi> From: Niklas Holsti Organization: Tidorum Ltd User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020623 Debian/1.0.0-0.woody.1 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada equivalent for C-pointer? References: <41E7F653.2F899F47@alfred-hilscher.de> <7107744.J8aUxUzYtP@linux1.krischik.com> <41EA81CC.8090804@nowhere.fi> <5149344.sr9q7XqTZ2@linux1.krischik.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Mon, 17 Jan 2005 17:33:41 +0200 NNTP-Posting-Host: 81.17.205.61 X-Complaints-To: newsmaster@saunalahti.com X-Trace: reader1.news.jippii.net 1105976009 81.17.205.61 (Mon, 17 Jan 2005 17:33:29 EET) NNTP-Posting-Date: Mon, 17 Jan 2005 17:33:29 EET Xref: g2news1.google.com comp.lang.ada:7871 Date: 2005-01-17T17:33:41+02:00 List-Id: Martin Krischik wrote: > Niklas Holsti wrote: > > >>Martin Krischik wrote: >> >>>Pascal Obry wrote: >>> >>> >>> >>>>Nick Roberts writes: >>>> >>>> >>>> >>>>>For example, if there is a C function defined thus: >>>>> >>>>> int wibble(int *n); >>>>> >>>>>then in Ada you can declare: >>>>> >>>>> function Wibble (Number: in out Interfaces.C.int); >>>>> pragma Import(C,Wibble,"wibble"); >>>>> >>>>>and it should work. (Obviously, you must link the C functions in >>>>>somehow.) >>>> >>>>I don't think so. Here Number will be passed by value, no? And you also >>>>need the returned value ;) >>> >>> >>>No all out and in out parameter are passed as pointer when pragma >>>Import(C is used - thats unless you use C_Pass_By_Copy. I have written >>>quite a lot of code interfaceing with C. >> >>But according to LRM B.3(68), for an "in out" parameter of an >>*elementary* type (as here) the pointer that is passed to the C function >>is a pointer to a *temporary copy* of the parameter (here Number), not a >>pointer to the parameter itself. This may not be what the C function >>expects. > > > Interesting. I have to think about that. Question is if the optimizer is > allowed to remove the copy. Well, as Nick Roberts pointed out LRM B.3(68) is only implementation advice, so to copy, or not to copy, is up to the implementation. >>For example, it would not make sense for "wibble" to save the >>pointer for later use. > > > Well storing a pointer to a stack object is allways wrong. No -- it's only wrong (in the sense of "does not work") to *use* such a pointer after the containing stack frame has returned. In this example, the stack frame that contains "Number" may exist for several calls of "wibble" or other C functions in the same library, and "wibble" might want to store the pointer somewhere (perhaps in an equally temporary data structure) for use by the other functions in the library. I've seen C libraries that do things like that, and it works, but of course it increases the risk that programming errors lead to use of invalid (dangling) pointers. > If you want to > store a pointer to the object neither "in out" nor "anomymous access" is > appropiate - a "pool access" should be used instead. And the Ada interface > should reflect that - if only to document propper use. I agree that "pool access" is a better solution when the C routine stores the pointer for a "long" time. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .