comp.lang.ada
 help / color / mirror / Atom feed
* Translate from C
@ 2005-04-09 19:30 Jean-Baptiste CAMPESATO
  2005-04-09 20:54 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jean-Baptiste CAMPESATO @ 2005-04-09 19:30 UTC (permalink / raw)


Hello,
I've this code in C : http://www.a2lf.org/util.c
and i want to translate it in Ada; 
A friend tried :
   procedure Copy
     (Source      : Address;
      Destination : Address;
      Bytes       : Natural)
   is
      S : String (1 .. Bytes);
      D : String (1 .. Bytes);
      for S'Address use Source;
      for D'Address use Destination;
   begin
      for I in S'Range loop
         D (I) := S (I);
      end loop;
   end Copy;

Do you think it's Ok ?

Thanks.



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

* Re: Translate from C
  2005-04-09 19:30 Translate from C Jean-Baptiste CAMPESATO
@ 2005-04-09 20:54 ` tmoran
       [not found] ` <gkvg51de5t83scllh0sp7go8nmcfe3ede9@4ax.com>
  2005-04-11 13:39 ` Steve
  2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2005-04-09 20:54 UTC (permalink / raw)


>I've this code in C : http://www.a2lf.org/util.c
>and i want to translate it in Ada;
  In Ada one would usually have "Destination := Source;" rather than
doing a procedure call.  But if it needs to be a procedure,
  procedure Copy(Source : in Interfaces.C.char_array;
                 Destination : out Interfaces.C.char_array;
                 N : in Natural) is
  begin
    Destination(Destination'first .. Destination'first+N-1)
      := Source(Source'first .. Source'first+N-1);
  end Copy;
assuming you want to deal with C arrays rather than Ada Strings.
Of course if you wanted to handle mixed types, eg, copy a 32 bit
float to 4 8-bit chars,  your friend's solution comes close.  However,
  S : String (1 .. Bytes);
may include bounds information as well as the data.  You can avoid that
possibility by making the S and D declarations as large, fixed size,
strings, where you are careful to only touch the first Bytes bytes, eg
  S : String (Positive);  -- fixed size array needs no dynamic bound info
  D : String (Positive);
To avoid possible initialization (ie, destruction) of S and D, you should
  pragma Import(C, S);
  pragma Import(C, D);
Also, in Ada you could replace the loop
      for I in S'Range loop
         D (I) := S (I);
      end loop;
by
      D(1 .. Bytes) := S(1 .. Bytes);



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

* Re: Translate from C
       [not found] ` <gkvg51de5t83scllh0sp7go8nmcfe3ede9@4ax.com>
@ 2005-04-10  7:47   ` Jean-Baptiste CAMPESATO
  0 siblings, 0 replies; 4+ messages in thread
From: Jean-Baptiste CAMPESATO @ 2005-04-10  7:47 UTC (permalink / raw)


Le Sun, 10 Apr 2005 01:36:45 +0000, Dennis Lee Bieber a ᅵcritᅵ:

> On Sat, 09 Apr 2005 21:30:30 +0200, Jean-Baptiste CAMPESATO
> <camje_lemon@a2lf.org> declaimed the following in comp.lang.ada:
> 
>> Hello,
>> I've this code in C : http://www.a2lf.org/util.c
>> and i want to translate it in Ada; 
>> A friend tried :
>>    procedure Copy
>>      (Source      : Address;
>>       Destination : Address;
>>       Bytes       : Natural)
>>    is
>>       S : String (1 .. Bytes);
>>       D : String (1 .. Bytes);
>>       for S'Address use Source;
>>       for D'Address use Destination;
>>    begin
>>       for I in S'Range loop
>>          D (I) := S (I);
>>       end loop;
>>    end Copy;
>> 
>> Do you think it's Ok ?
> 
> 	Probably not... You don't supply the type definition for
> "Address", nor an example of how the calling program will supply it...
> After all, there is no generic "pointer to anything" in Ada (that I know
> of, anyway)... Access types still are defined as pointers to /something/
> that the compiler can check.
> 
> 	Ignoring the problems of calling and passing pointers, the
> actual processing is probably not needed... What's wrong with just:
> 
> 	D := S;
> 
> and letting the compiler create the loop. After all, it already knows
> that D and S are both strings of a known size.
> 
> 	The better question is: WHY do you need this procedure? What do
> you expect to gain from it that you can't do with the language as-is? Or
> even more to the point, do without even using "pointer" semantics?


Ok :) Thanks



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

* Re: Translate from C
  2005-04-09 19:30 Translate from C Jean-Baptiste CAMPESATO
  2005-04-09 20:54 ` tmoran
       [not found] ` <gkvg51de5t83scllh0sp7go8nmcfe3ede9@4ax.com>
@ 2005-04-11 13:39 ` Steve
  2 siblings, 0 replies; 4+ messages in thread
From: Steve @ 2005-04-11 13:39 UTC (permalink / raw)


First:
  It is very un-Ada like to blindly copy values fromone address to another 
in Ada.
  In Ada you tend to think of things at a higher level than C.

Second:
  There are very rare cases where you do want to copy a block of memory from 
one address to another.
  In these cases there is a function in Interfaces.C.Pointers called 
"Copy_Array" that performs exactly this function.

It would be better if you described the functionality you're looking for. 
We often see questions on this group along the lines of "This is how I do it 
in C, how do I do it the same way in Ada?".  More often than not, the better 
question is "This is what I am doing in C, how do I do the same thing in 
Ada?".  Often Ada provides a better way of doing things that results in 
fewer bugs making it past the compiler.

Steve
(The Duck)


"Jean-Baptiste CAMPESATO" <camje_lemon@a2lf.org> wrote in message 
news:pan.2005.04.09.19.30.30.254343@a2lf.org...
> Hello,
> I've this code in C : http://www.a2lf.org/util.c
> and i want to translate it in Ada;
> A friend tried :
>   procedure Copy
>     (Source      : Address;
>      Destination : Address;
>      Bytes       : Natural)
>   is
>      S : String (1 .. Bytes);
>      D : String (1 .. Bytes);
>      for S'Address use Source;
>      for D'Address use Destination;
>   begin
>      for I in S'Range loop
>         D (I) := S (I);
>      end loop;
>   end Copy;
>
> Do you think it's Ok ?
>
> Thanks. 





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

end of thread, other threads:[~2005-04-11 13:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-09 19:30 Translate from C Jean-Baptiste CAMPESATO
2005-04-09 20:54 ` tmoran
     [not found] ` <gkvg51de5t83scllh0sp7go8nmcfe3ede9@4ax.com>
2005-04-10  7:47   ` Jean-Baptiste CAMPESATO
2005-04-11 13:39 ` Steve

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