comp.lang.ada
 help / color / mirror / Atom feed
From: dkusu@atc-1s.hac.com (David Kusuda)
Subject: Re: Pointers
Date: 13 Mar 1995 20:16:44 GMT
Date: 1995-03-13T20:16:44+00:00	[thread overview]
Message-ID: <3k297c$rg4@hacgate2.hac.com> (raw)
In-Reply-To: 794482546snz@jonf.demon.co.uk


In article 794482546snz@jonf.demon.co.uk, Jon@jonf.demon.co.uk (Jon Freeman) writes:
>Can anyone tell me if it is possible to read the contents of a specified
>memory location in ada or to read the contents of, say, an integer variable
>stored in a known location?
>Any help greatly appreciated.
>
>-- 
>Jon Freeman
>jon@jonf.demon.co.uk


There are (at least) two ways to accomplish what you want.  One may be technically
illegal depending on what you are trying to do, however.

The first method is to create an access type to the object in memory that you are
trying to examine.  You can then use an Unchecked_Conversion to convert the desired
address to your access type, for example:

  type Int_Ptr_Type is access Integer;
  function Addr_To_Ptr is new Unchecked_Conversion (
      Source => System.Address,
      Target => Int_Ptr_Type);

  Int_Ptr : Int_Ptr_Type := Addr_To_Ptr (Int_Ptr'Address);

You can also use an Unchecked_Conversion to point at a specific address too:

  function Int_To_Ptr is new Unchecked_Conversion (
      Source => Integer,
      Target => Int_Ptr_Type);

  Int_Ptr : Int_Ptr_Type := Int_To_Ptr (16#10000#);

You can then read or write to the specified memory location using regular access
dereferencing:

  Int_Val : Integer := Int_Ptr.all;
  .
  .
  .
  Int_Ptr.all := 5;

Now, for the potentially "illegal" method.  This method uses the address
representation clause.  Here, you would declare an object and then use an address
clause to locate the object in memory.

  Int_Val : Integer;
  for Int_Val use at at 16#10000#;

Accessing Int_Val has the effect of directly accessing the specified memory location.
If you were programming a memory mapped device, this would be a typical method of
doing that.

The illegality of the above is mentioned in the LRM (section 13.5):

  "Address clauses should not be used to achieve overlays of objects or overlays
   of program units.  Nor should a given interrupt be linked to more than one
   entry.  Any program using address clauses to achieve such effects is erroneous."

I hope this helps.


Dave  



  parent reply	other threads:[~1995-03-13 20:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-06  9:35 Pointers Jon Freeman
1995-03-06 12:27 ` Pointers Robert A Duff
1995-03-07 12:20 ` Pointers Robert Dewar
     [not found]   ` <3jki04$9bj@theopolis.orl.mmc.com>
1995-03-12 23:02     ` Pointers Robert A Duff
1995-03-13 15:54     ` Pointers Tucker Taft
1995-03-13 20:16 ` David Kusuda [this message]
  -- strict thread matches above, loose matches on Subject: below --
2006-06-01 19:46 pointers Schüle Daniel
2006-06-01 20:10 ` pointers Ludovic Brenta
2006-06-02  4:36 ` pointers Jeffrey R. Carter
replies disabled

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