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/11/06
Date: 1998-11-06T00:34:48+00:00	[thread overview]
Message-ID: <dale-0611981134580001@dale.bu.cs.rmit.edu.au> (raw)
In-Reply-To: 9v6hGdgMLuwN-pn2-dpQX3EjC2po4@dt182n2f.tampabay.rr.com

Craig Allen wrote:

" I've tried the suggestions given here, and failed miserably.  The 
  culprit in all cases was the fact that I'm using an Ada83 compiler"


Ah! This is a problem.

I would then suggest that if you are trying to emulate void *, then 
continue to use System.Address. 

You should also create a mapping from System.Address to the appropriate
pointer type by instantiating the Unchecked_Conversion function (which
may not do it exactly - there is no 100% guarentee that a pointer is
an address).

It is likely that you will have more success if you use a pointer to
a constrained type, as pointers to unconstrained types may well point
at the descriptor, rather than the actual data.

E.g.

   type My_String is array (1..100_000) of Character;
            -- any length will do, just ensure it's long enough for
            -- any problem you have to deal with.

   type Ptr is access My_String;

   function To_Ptr is new Unchecked_Conversion (
                           Source => System.Address,
                           Target => Ptr);


then...

      function Read_Bytes (...) return System.Address is
      begin
         blah blah blah;
         return Data'Address;
      end;

      X : Address := Read_Bytes (...);
      Y : Ptr := To_Ptr (Y);
      i : Positive := 1;
      Sentinel : constant Character := Ascii.nul;

   begin
      loop
         exit when Y (i) = Sentinel or i > My_String'Last;
         -- process item.
         i := i + 1;
      end loop;
   end;



Of course you will have to customise Unchecked_Conversion to deal
with any other pointer type.

Using this will mean you get around any problems of the compiler
expecting a static value for the address clause.

Beware though - you are deliberately subverting the type system. Are
you sure that this is necessary, or is there some aspect of the problem
which would be more elegantly described _within_ the type system offered
by Ada?


Dale




  parent reply	other threads:[~1998-11-06  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
1998-10-22  0:00   ` Hans Marqvardsen
1998-10-22  0:00   ` Hans Marqvardsen
1998-10-23  0:00     ` John McCabe
1998-10-23  0:00       ` dennison
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           ` Tucker Taft
1998-10-24  0:00           ` Robert A Duff
1998-10-23  0:00   ` David C. Hoos, Sr.
1998-10-23  0:00   ` David C. Hoos, Sr.
1998-10-23  0:00 ` Tom Moran
1998-10-23  0:00 ` Jeff Carter
1998-10-24  0:00   ` Dale Stanbrough
1998-10-25  0:00     ` dewar
1998-11-05  0:00 ` Craig Allen
1998-11-06  0:00   ` Tom Moran
1998-11-06  0:00   ` Dale Stanbrough [this message]
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