From: Vadim Godunko on
On Dec 24, 12:46 pm, "RasikaSriniva...(a)gmail.com"
<rasikasriniva...(a)gmail.com> wrote:
>
> type MyStruct is record ..... end record ;
> pragma Convention(C,MyStruct);
>
pragma Convention (C_Pass_by_Copy, MyStruct);
From: Niklas Holsti on
RasikaSrinivasan(a)gmail.com wrote:
> I am building an interface to a C library. This library uses structs
> like the following:
>
> typedef struct {
> x : int ;
> y : int } MyStruct ....
>
> While passing variables of this type to functions, the convention in C
> is to pass by value (best of my knowledge).

Correct, as I understand the C parameter-passing rules.

> however when I import a function like:
>
> type MyStruct is record ..... end record ;
> pragma Convention(C,MyStruct);
>
> procedure process(var : MyStruct);
> pragma Import(C,process);
>
> It appears that Ada passes the var by reference.

Yes, by default Ada considers records/struct under convention C as
pass-by-reference (LRM B.3 (69/2)). C programmers often choose to pass
structs by reference (pointer to struct) and the Ada Convention (C)
pragma has been defined accordingly. Or so I guess.

> So the question is - how c(an I force Ada (gnat) to pass a record by
> value?

Use Pragma Convention (C_Pass_By_Copy, MyStruct). See LRM B.3 (60.14/2)
and LRM B.3 (68.1/2).

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
From: Keith Thompson on
"Hibou57 (Yannick Duchêne)" <yannick_duchene(a)yahoo.fr> writes:
> On 24 déc, 10:46, "RasikaSriniva...(a)gmail.com"
> <rasikasriniva...(a)gmail.com> wrote:
>> I am building an interface to a C library. This library uses structs
>> like the following:
>>
>> typedef struct {
>>     x : int ;
>>     y : int }  MyStruct ....

(The correct syntax is:
typedef struct {
int x;
int y;
} MyStruct;
)

>> While passing variables of this type to functions, the convention in C
>> is to pass by value (best of my knowledge).

Yes. In fact, C doesn't have pass by reference as a language feature;
*all* function arguments are passed by value.

For structs, it's very common in C to do the equivalent of
pass-by-reference, by explicitly passing the address of the struct
object. This is partly because very old versions of C didn't
support struct parameters.

I find it a bit odd that Ada imposes a pass-by-pointer convention for
C structs.

(No, C arrays aren't passed by reference. C array expressions, in
most contexts, are implicitly converted to pointers; the resulting
pointer can be passed by value.)

[...]

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Hibou57 (Yannick Duchêne) on
On 24 déc, 13:34, Niklas Holsti <niklas.hol...(a)tidorum.invalid> wrote:
> RasikaSriniva...(a)gmail.com wrote:
> > I am building an interface to a C library. This library uses structs
> > like the following:
>
> > typedef struct {
> >     x : int ;
> >     y : int }  MyStruct ....
>
> > While passing variables of this type to functions, the convention in C
> > is to pass by value (best of my knowledge).
>
> Correct, as I understand the C parameter-passing rules.
Shame on me, you are both right. I was confused between standard
practice and language standard.

Language which allow C interface, like some Pascal dialects, do it
using implicit by reference structs, while with C, by reference
structs is done explicitly using a point type.
From: Robert A Duff on
Keith Thompson <kst-u(a)mib.org> writes:

> "Hibou57 (Yannick Duch�ne)" <yannick_duchene(a)yahoo.fr> writes:
>> On 24 d�c, 10:46, "RasikaSriniva...(a)gmail.com"
>> <rasikasriniva...(a)gmail.com> wrote:
>>> I am building an interface to a C library. This library uses structs
>>> like the following:
>>>
>>> typedef struct {
>>> � � x : int ;
>>> � � y : int } �MyStruct ....
>
> (The correct syntax is:
> typedef struct {
> int x;
> int y;
> } MyStruct;
> )

Right.

>>> While passing variables of this type to functions, the convention in C
>>> is to pass by value (best of my knowledge).
>
> Yes. In fact, C doesn't have pass by reference as a language feature;
> *all* function arguments are passed by value.
>
> For structs, it's very common in C to do the equivalent of
> pass-by-reference, by explicitly passing the address of the struct
> object. This is partly because very old versions of C didn't
> support struct parameters.

Which version of C (when?) introduced (by-copy) struct params?

> I find it a bit odd that Ada imposes a pass-by-pointer convention for
> C structs.

It was a mistake. Partly my fault. I was thinking that it's common in
C to pass struct params by explicitly passing a pointer-to-struct, so
Ada should mimic that. Bad idea. By the time we realized the mistake,
it was too late to fix (compatibility!), so we invented C_Pass_By_Copy
as a workaround.

> (No, C arrays aren't passed by reference. C array expressions, in
> most contexts, are implicitly converted to pointers; the resulting
> pointer can be passed by value.)

Yeah. In other words, C arrays aren't passed, period.
That's another bad idea, and it's not my fault. ;-)

- Bob