comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing to C
@ 2005-02-08 18:07 Garry
  2005-02-08 18:43 ` Georg Bauhaus
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Garry @ 2005-02-08 18:07 UTC (permalink / raw)


I'm just starting in ADA.  Can anyone explain why I have to define a
dummy procedure in my hsdbg package to get my code to work?

Error without dummy procedure:
hsdbg.ads:4:09: package "HSDBG" does not allow a body
hsdbg.ads:4:09: remove incorrect body in file "hsdbg.adb"
gnatmake: "hsdbg.ads" compilation error
error: "hsdbg.adb" must be recompiled ("hsdbg.ads" has been modified)
gnatlink: Failed to open binder output

Code follows:
/* Start of marktime.c */
#include <stdio.h>

void marktime(char  str[255])
{
   printf("marktime: str='%s'\n",str);
}
/* End of marktime.c */



-- Start of hsdbg.ads
--
with Interfaces.C;
with Interfaces.C.Strings;

package HSDBG is
--  Dummy procedure?
      type DUMB is digits 5 range 0.0 .. 1.0E10;
      procedure DUMMY (B : DUMB);
--  End Dummy procedure
      procedure marktime(Variable : Interfaces.C.Strings.chars_ptr);
      pragma Import(C, marktime, "marktime");
end HSDBG;
-- End of hsdbg.ads



-- Start of hsdbg.adb
--
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
--
--  Define package body
--
package body HSDBG is
--
--
--  dummy procedure?
  procedure DUMMY(B : DUMB) is
  begin
     put("In Dummy");
  end DUMMY;
--  end dummy procedure

   procedure marktime(Variable : String) is

      procedure marktime(Variable : chars_ptr);
      pragma Import(C, marktime, "marktime");

      Variable_In_C_Format : chars_ptr := New_String(Variable);


   begin
      marktime(Variable_In_C_Format);
      Free(Variable_In_C_Format);
   end marktime;


 end HSDBG;
-- End of hsdbg.adb




-- Start of tst.adb
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with hsdbg; use hsdbg;

procedure tst is

  C_Format : chars_ptr := New_String("This is a string in ADA that is
written by the marktime C procedure");

begin
   Put_Line ("test started");
   marktime(C_Format);

end tst;
-- End of tst.adb




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-08 18:07 Interfacing to C Garry
@ 2005-02-08 18:43 ` Georg Bauhaus
  2005-02-08 19:01 ` Ludovic Brenta
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 17+ messages in thread
From: Georg Bauhaus @ 2005-02-08 18:43 UTC (permalink / raw)


Garry wrote:
 
> -- Start of hsdbg.ads
> --
> with Interfaces.C;
> with Interfaces.C.Strings;
> 
> package HSDBG is
> --  Dummy procedure?
>       type DUMB is digits 5 range 0.0 .. 1.0E10;
>       procedure DUMMY (B : DUMB);
> --  End Dummy procedure
>       procedure marktime(Variable : Interfaces.C.Strings.chars_ptr);
>       pragma Import(C, marktime, "marktime");
> end HSDBG;
> -- End of hsdbg.ads
> 

You don't need a dummy procedure, and according to the spec you do
not need a body either.
  The reason is that in general a body is there to complete
the declarations in the spec. As the only subprogram in
the spec, "marktime", is imported, there is nothing to complete
in this sense. Now to require a body, the Ada LRM has this to say
(Section 7.2)

pragma Elaborate_Body
     can be used to require a library_unit_declaration to have a body,
     ... if it would not otherwise require one.


-- Georg



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-08 18:07 Interfacing to C Garry
  2005-02-08 18:43 ` Georg Bauhaus
@ 2005-02-08 19:01 ` Ludovic Brenta
  2005-02-08 19:27 ` Martin Krischik
  2005-02-08 20:50 ` Björn Lundin
  3 siblings, 0 replies; 17+ messages in thread
From: Ludovic Brenta @ 2005-02-08 19:01 UTC (permalink / raw)


You're writing a binding to the "marktime" function.  Fine.  There are
problems with your binding, however.

In your package spec, you declare a procedure Marktime, which you
import.  The import is a "completion" in Ada reference manual terms;
this means that the procedure "marktime" is completely known after the
pragma Import line.

Per ARM 3.1.11(7), "At most one completion is allowed for a given
declaration."  Thus, the compiler will not look for other completions;
the procedure does not need a body (and indeed, providing a body is
illegal).

Except for the dummy procedure, nothing in your package spec needs a
completion (i.e. everything is completely defined by the spec).
Therefore, your package spec does not need a package body.

Per ARM 7.2(4), "A library package_declaration [...] shall not have a
body unless it requires a body; pragma Elaborate_Body can be used to
require a library_unit_declaration to have a body, if it would not
otherwise require one."

Here, I think the underlying problem is that you are trying to provide
two completions for procedure Marktime: the pragma Import and the
body.  You must choose one and remove the other.

-- 
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-08 18:07 Interfacing to C Garry
  2005-02-08 18:43 ` Georg Bauhaus
  2005-02-08 19:01 ` Ludovic Brenta
@ 2005-02-08 19:27 ` Martin Krischik
  2005-02-08 20:50 ` Björn Lundin
  3 siblings, 0 replies; 17+ messages in thread
From: Martin Krischik @ 2005-02-08 19:27 UTC (permalink / raw)


Garry wrote:

> I'm just starting in ADA.  Can anyone explain why I have to define a
> dummy procedure in my hsdbg package to get my code to work?

Because you misunderstood something. Of corse you don't need DUMMY.
 
> Error without dummy procedure:
> hsdbg.ads:4:09: package "HSDBG" does not allow a body
> hsdbg.ads:4:09: remove incorrect body in file "hsdbg.adb"
> gnatmake: "hsdbg.ads" compilation error
> error: "hsdbg.adb" must be recompiled ("hsdbg.ads" has been modified)
> gnatlink: Failed to open binder output
> 
> Code follows:
> /* Start of marktime.c */
> #include <stdio.h>
> 
> void marktime(char  str[255])
> {
>    printf("marktime: str='%s'\n",str);
> }
> /* End of marktime.c */
> 
> 
> 
> -- Start of hsdbg.ads
> --
> with Interfaces.C;
> with Interfaces.C.Strings;
> 
> package HSDBG is
> --  Dummy procedure?
>       type DUMB is digits 5 range 0.0 .. 1.0E10;
>       procedure DUMMY (B : DUMB);

As Georg pointed out you need:

  pragma Elaborate_Body;

or prehaps:

  procedure marktime(Variable : String);

that would export the Ada version of marktime(Variable : String);

> --  End Dummy procedure
>       procedure marktime(Variable : Interfaces.C.Strings.chars_ptr);
>       pragma Import(C, marktime, "marktime");
> end HSDBG;
> -- End of hsdbg.ads
> 
> 
> 
> -- Start of hsdbg.adb
> --
> with Ada.Text_IO; use Ada.Text_IO;
> with Interfaces.C.Strings; use Interfaces.C.Strings;
> --
> --  Define package body
> --
> package body HSDBG is
> --
> --
> --  dummy procedure?
>   procedure DUMMY(B : DUMB) is
>   begin
>      put("In Dummy");
>   end DUMMY;
> --  end dummy procedure
> 
>    procedure marktime(Variable : String) is
> 
>       procedure marktime(Variable : chars_ptr);
>       pragma Import(C, marktime, "marktime");

Well you don't need to repeat the C declarartion here. You should decide for
one and delete the other. Which one is design decision - you have to take
that yourself

>       Variable_In_C_Format : chars_ptr := New_String(Variable);
> 
> 
>    begin
>       marktime(Variable_In_C_Format);
>       Free(Variable_In_C_Format);
>    end marktime;
> 
> 
>  end HSDBG;
> -- End of hsdbg.adb
> 
> 
> 
> 
> -- Start of tst.adb
> with Ada.Text_IO; use Ada.Text_IO;
> with Interfaces.C.Strings; use Interfaces.C.Strings;
> with hsdbg; use hsdbg;
> 
> procedure tst is
> 
>   C_Format : chars_ptr := New_String("This is a string in ADA that is
> written by the marktime C procedure");
> 
> begin
>    Put_Line ("test started");
>    marktime(C_Format);
> 
> end tst;
> -- End of tst.adb

Well it look all very confusing here. It looks like you have two test codes
and two C binding.

One by which the pragma Import shows in the body of hsdbg - one where is
shows in the spec of hsdbg.

One where you test with a test procedure one where you test with a package
body.

In both cases both designs are possible and have distict advantages and
disadvantges. If you have a tutor then talk with him/her and then make a
decision.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-08 18:07 Interfacing to C Garry
                   ` (2 preceding siblings ...)
  2005-02-08 19:27 ` Martin Krischik
@ 2005-02-08 20:50 ` Björn Lundin
  2005-02-09  7:27   ` Anders Wirzenius
  3 siblings, 1 reply; 17+ messages in thread
From: Björn Lundin @ 2005-02-08 20:50 UTC (permalink / raw)
  To: comp.lang.ada

tisdag 08 februari 2005 19:07 skrev Garry:
> I'm just starting in ADA.  Can anyone explain why I have to define a
> dummy procedure in my hsdbg package to get my code to work?

My guess is that you don't know what you want.  Your code slightly rewritten 
perhaps make sense?
I took the liberty to separate your marktime procedures in a 'thin' (c-close) 
and a thick (ada -close) version

----------
bnl@della2 slask]$ cat *.ad[bs] *.c

/* Start of marktime.c */
#include <stdio.h>
void marktime(char str[255]) {
  printf("marktime: str='%s'\n",str);
}

-- Start of hsdbg.ads
with Interfaces.C;
with Interfaces.C.Strings;

package HSDBG is
  procedure Marktime_Thin(Variable : Interfaces.C.Strings.chars_ptr);
  procedure Marktime_Thick(Variable : String);
private
  pragma Import(C, Marktime_Thin, "marktime");
end HSDBG;


-- Start of hsdbg.adb
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;

package body HSDBG is
  procedure Marktime_Thick(Variable : String) is
   Variable_In_C_Format : Chars_ptr := New_String(Variable);
  begin
   Marktime_Thin(Variable_In_C_Format);
   Free(Variable_In_C_Format);
  end Marktime_Thick;
end HSDBG;
-- End of hsdbg.adb

-- Start of tst.adb
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with hsdbg; use hsdbg;

procedure Tst is
 C_Format : chars_ptr := New_String("This is a string in Ada that is written 
bythe marktime(C) procedure");
 Ada_Format : String := "This is a string in Ada that is written by the 
marktime(Ada) procedure";
begin
  Put_Line ("test started");
  Marktime_Thin(C_Format);
  Marktime_Thick(Ada_Format);
end Tst;
[bnl@della2 slask]$     
----------

compile and link and testrun gives:

[bnl@della2 slask]$ gcc -c marktime.c
[bnl@della2 slask]$ gnatmake tst -largs marktime.o
gnatbind -x tst.ali
gnatlink tst.ali marktime.o
[bnl@della2 slask]$ ./tst
test started
marktime: str='This is a string in Ada that is written by the marktime(C) 
procedure'
marktime: str='This is a string in Ada that is written by the marktime(Ada) 
procedure'
[bnl@della2 slask]$ 


So you don't need a dummy procedure. 

/Björn




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-08 20:50 ` Björn Lundin
@ 2005-02-09  7:27   ` Anders Wirzenius
  2005-02-09 18:53     ` Garry
  0 siblings, 1 reply; 17+ messages in thread
From: Anders Wirzenius @ 2005-02-09  7:27 UTC (permalink / raw)


Bj�rn Lundin <bnl@tiscali.se> writes:

> tisdag 08 februari 2005 19:07 skrev Garry: > I'm just starting in ADA.
> Can anyone explain why I have to define a > dummy procedure in my
> hsdbg package to get my code to work?
> 
> My guess is that you don't know what you want.  Your code slightly
> rewritten perhaps make sense?  I took the liberty to separate your
> marktime procedures in a 'thin' (c-close) and a thick (ada -close)
> version
> 

... (code deleted)

> 
> So you don't need a dummy procedure. 
> 
> /Bj�rn

This is an excellent example of a positive attitude to help a newbie!

Congratulations, Bj�rn!

Anders



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-09  7:27   ` Anders Wirzenius
@ 2005-02-09 18:53     ` Garry
  2005-02-09 21:35       ` Björn Lundin
  0 siblings, 1 reply; 17+ messages in thread
From: Garry @ 2005-02-09 18:53 UTC (permalink / raw)



Anders Wirzenius wrote:
> Björn Lundin <bnl@tiscali.se> writes:
>
> > tisdag 08 februari 2005 19:07 skrev Garry: > I'm just starting in
ADA.
> > Can anyone explain why I have to define a > dummy procedure in my
> > hsdbg package to get my code to work?
> >
> > My guess is that you don't know what you want.  Your code slightly
> > rewritten perhaps make sense?  I took the liberty to separate your
> > marktime procedures in a 'thin' (c-close) and a thick (ada -close)
> > version
> >
>
> ... (code deleted)
>
> >
> > So you don't need a dummy procedure.
> >
> > /Björn
>
> This is an excellent example of a positive attitude to help a newbie!
>
> Congratulations, Björn!
>
> Anders

This was great help!  I want to thank everyone who replied to my post!
I appreciate the time you experts expended in helping me.

Thanks Again!

Garry




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2005-02-09 18:53     ` Garry
@ 2005-02-09 21:35       ` Björn Lundin
  0 siblings, 0 replies; 17+ messages in thread
From: Björn Lundin @ 2005-02-09 21:35 UTC (permalink / raw)
  To: comp.lang.ada

onsdag 09 februari 2005 19:53 skrev Garry:
> Anders Wirzenius wrote:
> > This is an excellent example of a positive attitude to help a newbie!
> >
> > Congratulations, Björn!
> >
> > Anders

Isn't that what this list is for? To be helped AND help? :)

>
> This was great help!  I want to thank everyone who replied to my post!
> I appreciate the time you experts expended in helping me.

Glad to be of help
/Björn



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Interfacing to C
@ 2009-12-24  9:46 RasikaSrinivasan@gmail.com
  2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: RasikaSrinivasan@gmail.com @ 2009-12-24  9:46 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24  9:46 RasikaSrinivasan@gmail.com
@ 2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
  2009-12-25  2:07   ` Keith Thompson
  2009-12-24 12:34 ` Niklas Holsti
  2009-12-24 12:35 ` Vadim Godunko
  2 siblings, 1 reply; 17+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-12-24 10:01 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24  9:46 RasikaSrinivasan@gmail.com
  2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
@ 2009-12-24 12:34 ` Niklas Holsti
  2009-12-24 14:26   ` RasikaSrinivasan@gmail.com
  2009-12-28  7:20   ` Hibou57 (Yannick Duchêne)
  2009-12-24 12:35 ` Vadim Godunko
  2 siblings, 2 replies; 17+ messages in thread
From: Niklas Holsti @ 2009-12-24 12:34 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24  9:46 RasikaSrinivasan@gmail.com
  2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
  2009-12-24 12:34 ` Niklas Holsti
@ 2009-12-24 12:35 ` Vadim Godunko
  2 siblings, 0 replies; 17+ messages in thread
From: Vadim Godunko @ 2009-12-24 12:35 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24 12:34 ` Niklas Holsti
@ 2009-12-24 14:26   ` RasikaSrinivasan@gmail.com
  2009-12-28  7:20   ` Hibou57 (Yannick Duchêne)
  1 sibling, 0 replies; 17+ messages in thread
From: RasikaSrinivasan@gmail.com @ 2009-12-24 14:26 UTC (permalink / raw)


On Dec 24, 7:34 am, 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.
>
> > 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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
@ 2009-12-25  2:07   ` Keith Thompson
  2009-12-31 23:18     ` Robert A Duff
  0 siblings, 1 reply; 17+ messages in thread
From: Keith Thompson @ 2009-12-25  2:07 UTC (permalink / raw)


"Hibou57 (Yannick Duchêne)" <yannick_duchene@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) kst-u@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"



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-24 12:34 ` Niklas Holsti
  2009-12-24 14:26   ` RasikaSrinivasan@gmail.com
@ 2009-12-28  7:20   ` Hibou57 (Yannick Duchêne)
  1 sibling, 0 replies; 17+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-12-28  7:20 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-25  2:07   ` Keith Thompson
@ 2009-12-31 23:18     ` Robert A Duff
  2010-01-01 18:02       ` Keith Thompson
  0 siblings, 1 reply; 17+ messages in thread
From: Robert A Duff @ 2009-12-31 23:18 UTC (permalink / raw)


Keith Thompson <kst-u@mib.org> writes:

> "Hibou57 (Yannick Duch�ne)" <yannick_duchene@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



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: Interfacing to C
  2009-12-31 23:18     ` Robert A Duff
@ 2010-01-01 18:02       ` Keith Thompson
  0 siblings, 0 replies; 17+ messages in thread
From: Keith Thompson @ 2010-01-01 18:02 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Keith Thompson <kst-u@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.)

-- 
Keith Thompson (The_Other_Keith) kst-u@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"



^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2010-01-01 18:02 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-08 18:07 Interfacing to C Garry
2005-02-08 18:43 ` Georg Bauhaus
2005-02-08 19:01 ` Ludovic Brenta
2005-02-08 19:27 ` Martin Krischik
2005-02-08 20:50 ` Björn Lundin
2005-02-09  7:27   ` Anders Wirzenius
2005-02-09 18:53     ` Garry
2005-02-09 21:35       ` Björn Lundin
  -- strict thread matches above, loose matches on Subject: below --
2009-12-24  9:46 RasikaSrinivasan@gmail.com
2009-12-24 10:01 ` Hibou57 (Yannick Duchêne)
2009-12-25  2:07   ` Keith Thompson
2009-12-31 23:18     ` Robert A Duff
2010-01-01 18:02       ` Keith Thompson
2009-12-24 12:34 ` Niklas Holsti
2009-12-24 14:26   ` RasikaSrinivasan@gmail.com
2009-12-28  7:20   ` Hibou57 (Yannick Duchêne)
2009-12-24 12:35 ` Vadim Godunko

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