comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <nobody@nowhere.fi>
Subject: Re: Ada equivalent for C-pointer?
Date: Mon, 17 Jan 2005 17:33:41 +0200
Date: 2005-01-17T17:33:41+02:00	[thread overview]
Message-ID: <41EBDAD5.4000703@nowhere.fi> (raw)
In-Reply-To: 5149344.sr9q7XqTZ2@linux1.krischik.com

Martin Krischik wrote:
> Niklas Holsti wrote:
> 
> 
>>Martin Krischik wrote:
>>
>>>Pascal Obry wrote:
>>>
>>>
>>>
>>>>Nick Roberts <nick.roberts@acm.org> 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
       .      @       .




  reply	other threads:[~2005-01-17 15:33 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-14 16:41 Ada equivalent for C-pointer? Alfred Hilscher
2005-01-14 17:00 ` Duncan Sands
2005-01-14 20:05   ` tmoran
2005-01-15  5:00     ` Brian May
2005-01-14 22:33 ` Keith Thompson
2005-01-14 23:03   ` Stephen Leake
2005-01-15 15:51     ` Nick Roberts
2005-01-15 18:54       ` tmoran
2005-01-16  1:43         ` Keith Thompson
2005-01-17 21:35       ` Randy Brukardt
2005-01-15  9:09 ` Martin Krischik
2005-01-15 16:03 ` Nick Roberts
2005-01-15 16:27   ` Pascal Obry
2005-01-15 16:50     ` Nick Roberts
2005-01-15 17:11       ` Simon Wright
2005-01-15 18:46         ` Nick Roberts
2005-01-15 17:49       ` Pascal Obry
2005-01-16  8:44       ` Martin Krischik
2005-01-16  9:58         ` Pascal Obry
2005-01-16 11:07           ` Martin Krischik
2005-01-16  8:36     ` Martin Krischik
2005-01-16 15:01       ` Niklas Holsti
2005-01-16 16:02         ` Martin Krischik
2005-01-17 15:33           ` Niklas Holsti [this message]
2005-01-16 20:39         ` Nick Roberts
2005-01-17 21:38     ` Randy Brukardt
2005-01-17 21:45       ` Pascal Obry
2005-01-17 13:11 ` Alfred Hilscher
2005-01-18  7:31   ` Keith Thompson
replies disabled

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