comp.lang.ada
 help / color / mirror / Atom feed
From: dale@cs.rmit.edu.au (Dale Stanbrough)
Subject: Re: simple question - how to emulate void * ?
Date: 1998/10/23
Date: 1998-10-23T03:16:38+00:00	[thread overview]
Message-ID: <dale-2310981318260001@dale.ppp.cs.rmit.edu.au> (raw)
In-Reply-To: 9v6hGdgMLuwN-pn2-Oc41W71Dq3U9@dt182n2f.tampabay.rr.com

Craig Allen wrote:

 Well, yes, I'm new to ada, and coming from C.
 
 Now, I have some simple functionality that I'd like to see, and can't 
 figure out how to do it in Ada.
 
 in C, it goes like this:
 
 /* libfile.c */
 
 void print_bytes(void *buf, int len)
 {
         int i;
         char *c = buf;
         for (i=0; i<len; ++i) {
                 putchar(c[i]);
         }
 
         return;
 }
 
 /* app.c */
         ...
         struct any_struct data;
         print_bytes(&data, sizeof (struct data));
         ...
 
 The lib can access byte for byte the data passed in regardless of the 
 app's struct type.  Now, Ada being such a strongly typed language, I 
 have tried different things but just can't seem to figure out how to 
 get this lib routine to work on my buffer, regardless of its type.  
 I've come close with some generic functions, but can't seem to get the
 syntax acceptable for the compiler.
 Can someone point me in the right direction?"


Try using the object's address...

--------------------------------------------------------
with System;                  use System;
with System.Storage_Elements; use System.Storage_Elements;
with System.Address_To_Access_Conversions;
with text_io;                 use text_IO;


procedure PB is

   package Char_Address is new System.Address_To_Access_Conversions (Character);
   use Char_Address;


   procedure Print_Bytes (Buffer : System.Address; Length : Integer)
   is
      Temp : System.Address;
   begin
      Temp := Buffer;
      for i in 1..Length loop
         Text_IO.Put (To_Pointer (Temp).all);
         Temp := Temp + 1;
      end loop;
   end Print_Bytes;



   X : String := "Hello There";
begin

   Print_Bytes (X'Address, X'Length);

end;
--------------------------------------------------------
(compiled and works using Gnat3.10, Sparc Solaris).

Also the function "To_Pointer" is an intrinsic function, so it won't
incur any overhead (that i can see) for being called. All the package
Address_To_Access_Conversion does is explicity enforces what C does
implicity, and at the same cost (someone please correct me if i am 
wrong)).

Note that X'Length will give you the number of _Components_ in an
array, not the length (in bytes) of the array.

You could also use streams, which (IMHO) is a much more versatile 
tool.

Dale




  reply	other threads:[~1998-10-23  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-10-23  0:00 simple question - how to emulate void * ? Craig Allen
1998-10-23  0:00 ` Dale Stanbrough [this message]
1998-10-22  0:00   ` Hans Marqvardsen
1998-10-23  0:00     ` John McCabe
1998-10-23  0:00       ` Ed Falis
1998-10-23  0:00         ` dennison
1998-10-24  0:00           ` Joe Wisniewski
1998-10-25  0:00           ` dewar
1998-10-24  0:00         ` Dale Stanbrough
1998-10-24  0:00           ` Robert A Duff
1998-10-24  0:00           ` Tucker Taft
1998-10-23  0:00       ` dennison
1998-10-22  0:00   ` Hans Marqvardsen
1998-10-23  0:00   ` David C. Hoos, Sr.
1998-10-23  0:00   ` David C. Hoos, Sr.
1998-10-23  0:00 ` Jeff Carter
1998-10-24  0:00   ` Dale Stanbrough
1998-10-25  0:00     ` dewar
1998-10-23  0:00 ` Tom Moran
1998-11-05  0:00 ` Craig Allen
1998-11-06  0:00   ` Tom Moran
1998-11-06  0:00   ` Dale Stanbrough
1998-11-06  0:00     ` Matthew Heaney
1998-11-06  0:00       ` dewarr
replies disabled

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