Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interfacing to C

26 views
Skip to first unread message

rasikasr...@gmail.com

unread,
Dec 24, 2009, 4:46:05 AM12/24/09
to
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).

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. (Well - I am not able
to tell for sure - just that modifying the binding as follows :

procedure process(varx : int; vary : int);
pragma Import(C,process) ;

works and the previous binding did not.

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

thanks for pointers, srini

Hibou57 (Yannick Duchêne)

unread,
Dec 24, 2009, 5:01:47 AM12/24/09
to
On 24 déc, 10:46, "RasikaSriniva...@gmail.com"

<rasikasriniva...@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).
To mine, this is done by reference.
The compiler may apply some optimization on static functions, i.e. the
ones which are not exported, if it does, it should not with public
functions.

> 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.

Your Ada compiler apply the C convention, as you require it to do :p

> (Well - I am not able
> to tell for sure - just that modifying the binding as follows :
>
> procedure process(varx : int; vary : int);
> pragma Import(C,process) ;
>
> works and the previous binding did not.

Why would you want to do so ?

Did you checked the function you want to access from Ada really expect
this ?

If it is, then perhaps it does not really use the C convention.


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

I do not know, I will check about it (I'm mostly sure it is not
possible), unless someone else come with a better answer.

Have a nice day and a Merry Christmas

Vadim Godunko

unread,
Dec 24, 2009, 7:35:03 AM12/24/09
to
On Dec 24, 12:46 pm, "RasikaSriniva...@gmail.com"

<rasikasriniva...@gmail.com> wrote:
>
> type MyStruct is record ..... end record ;
> pragma Convention(C,MyStruct);
>
pragma Convention (C_Pass_by_Copy, MyStruct);

Niklas Holsti

unread,
Dec 24, 2009, 7:34:17 AM12/24/09
to
RasikaSr...@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
. @ .

rasikasr...@gmail.com

unread,
Dec 24, 2009, 9:26:34 AM12/24/09
to
On Dec 24, 7:34 am, Niklas Holsti <niklas.hol...@tidorum.invalid>
wrote:

Vadim and Niklas

Thank you so much, srini

Keith Thompson

unread,
Dec 24, 2009, 9:07:52 PM12/24/09
to
"Hibou57 (Yannick Duchêne)" <yannick...@yahoo.fr> writes:
> On 24 déc, 10:46, "RasikaSriniva...@gmail.com"
> <rasikasriniva...@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) ks...@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"

Hibou57 (Yannick Duchêne)

unread,
Dec 28, 2009, 2:20:40 AM12/28/09
to
On 24 déc, 13:34, Niklas Holsti <niklas.hol...@tidorum.invalid> wrote:

> RasikaSriniva...@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.

Robert A Duff

unread,
Dec 31, 2009, 6:18:34 PM12/31/09
to
Keith Thompson <ks...@mib.org> writes:

> "Hibou57 (Yannick Duch�ne)" <yannick...@yahoo.fr> writes:
>> On 24 d�c, 10:46, "RasikaSriniva...@gmail.com"


>> <rasikasriniva...@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

Keith Thompson

unread,
Jan 1, 2010, 1:02:01 PM1/1/10
to
Robert A Duff <bob...@shell01.TheWorld.com> writes:
> Keith Thompson <ks...@mib.org> writes:
[...]

>> 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?

It appeared in the 1989 ANSI C standard. It was undoubtedly
implemented in some compilers a few years before that. K&R1
(Kernighan & Ritchie, "The C Programming Language, 1st edition, 1978)
had structs, but didn't allow them to be assigned or passed as
arguments.

To be clear, C has *never* allowed structs to be passed by reference
implicitly, though it's always allowed the equivalent by explicit
coding.

>> 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. ;-)

Right, C arrays are very much second-class citizens, almost
always manipulated indirectly via pointers. (There's a common
misconception that C arrays and pointers are the same thing.
They most definitely are not. See section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com>, for the details.)

0 new messages