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
> 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
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
. @ .
Vadim and Niklas
Thank you so much, srini
(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"
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.
> "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
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.)