comp.lang.ada
 help / color / mirror / Atom feed
* Pointers
@ 1995-03-06  9:35 Jon Freeman
  1995-03-06 12:27 ` Pointers Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jon Freeman @ 1995-03-06  9:35 UTC (permalink / raw)


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



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

* Re: Pointers
  1995-03-06  9:35 Pointers Jon Freeman
@ 1995-03-06 12:27 ` Robert A Duff
  1995-03-07 12:20 ` Pointers Robert Dewar
  1995-03-13 20:16 ` Pointers David Kusuda
  2 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 1995-03-06 12:27 UTC (permalink / raw)


In article <794482546snz@jonf.demon.co.uk>,
Jon Freeman <Jon@jonf.demon.co.uk> wrote:
>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?

Declare an integer (or whatever) variable, and then use an
address_clause to tell the compiler where to put it.
Then read it as usual.

Alternatively, you could use the Address_To_Access_Conversions library
unit (of Ada 95).

- Bob



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

* Re: Pointers
  1995-03-06  9:35 Pointers Jon Freeman
  1995-03-06 12:27 ` Pointers Robert A Duff
@ 1995-03-07 12:20 ` Robert Dewar
       [not found]   ` <3jki04$9bj@theopolis.orl.mmc.com>
  1995-03-13 20:16 ` Pointers David Kusuda
  2 siblings, 1 reply; 9+ messages in thread
From: Robert Dewar @ 1995-03-07 12:20 UTC (permalink / raw)


Jon, we have to ask why a simple address clause

   for x use at ...

does not work for you, since this is the obvious way of reading a specific
memory address. You have to consult your implementor on how to represent
address values, since this is obviously target dependent.




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

* Re: Pointers
       [not found]   ` <3jki04$9bj@theopolis.orl.mmc.com>
@ 1995-03-12 23:02     ` Robert A Duff
  1995-03-13 15:54     ` Pointers Tucker Taft
  1 sibling, 0 replies; 9+ messages in thread
From: Robert A Duff @ 1995-03-12 23:02 UTC (permalink / raw)


In article <3jki04$9bj@theopolis.orl.mmc.com>,
Bob Gilbert <rgilbert@orl.mmc.com> wrote:
>In my experience I have found several cases where the simple "for x use at .."
>will not work.  One problem is that with most comilers which support the 
>address clause, will perform a write to the memory location (initialization 
>I suppose) during elaboration.  This can cause problems with "read only" 
>addresses. ...

In Ada 95, you can turn off that behavior with a pragma Import.
See RM-B.1(38).

> ...In these situations I 
>had to resort to using access types, unchecked_conversion, and the knowledge
>that the comiler implements access types as addresses, to work around the 
>problem.  

You don't normally need to do that anymore, but if you ever *do* want to
deal with access values as addresses, use the
Address_To_Access_Conversions package -- you no longer need to make
*assumptions* about the representation of the access values.

- Bob



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

* Re: Pointers
       [not found]   ` <3jki04$9bj@theopolis.orl.mmc.com>
  1995-03-12 23:02     ` Pointers Robert A Duff
@ 1995-03-13 15:54     ` Tucker Taft
  1 sibling, 0 replies; 9+ messages in thread
From: Tucker Taft @ 1995-03-13 15:54 UTC (permalink / raw)


Bob Gilbert (rgilbert@orl.mmc.com) wrote:
: In article 2er@gnat.cs.nyu.edu, dewar@cs.nyu.edu (Robert Dewar) writes:
: ->Jon, we have to ask why a simple address clause
: ->
: ->   for x use at ...
: ->
: ->does not work for you, since this is the obvious way of reading a specific
: ->memory address. You have to consult your implementor on how to represent
: ->address values, since this is obviously target dependent.

: Not to answer for Jon, but.....

: In my experience I have found several cases where the simple "for x use at .."
: will not work.  One problem is that with most comilers which support the 
: address clause, will perform a write to the memory location (initialization 
: I suppose) during elaboration.  This can cause problems with "read only" 
: addresses.  

In Ada 95, you can specify "pragma Import(Ada, <object-name>);"
which indicates that *no* default initialization is to be performed
by the compiler-generated code.  This is generally desirable when
using an address clause to associate a name with a machine register
of some sort.

Also, in Ada 95 there are language-defined functions for converting
integers to addresses.  Finally, the preferred syntax for
specifying the address of an object uses "for ...'Address use ...",
such as:

   for Console_Status_Register'Address use 
     System.Storage_Elements.To_Address(16#FFC0#);

The "for ... use at ..." syntax is still retained for upward 
compatibility reasons.

: -Bob

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



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

* Re: Pointers
  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
@ 1995-03-13 20:16 ` David Kusuda
  2 siblings, 0 replies; 9+ messages in thread
From: David Kusuda @ 1995-03-13 20:16 UTC (permalink / raw)



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  



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

* pointers
@ 2006-06-01 19:46 Schüle Daniel
  2006-06-01 20:10 ` pointers Ludovic Brenta
  2006-06-02  4:36 ` pointers Jeffrey R. Carter
  0 siblings, 2 replies; 9+ messages in thread
From: Schüle Daniel @ 2006-06-01 19:46 UTC (permalink / raw)


Hello,

are there some page out there explaining Ada pointers for
C programmers?

Ada pitfalls for C mind set etc

especially wheather or not object should explicitely
allow to point to it
how to create (heap) memory pools
how to point to stack objects ... and so on

Thx

Regards, Daniel



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

* Re: pointers
  2006-06-01 19:46 pointers Schüle Daniel
@ 2006-06-01 20:10 ` Ludovic Brenta
  2006-06-02  4:36 ` pointers Jeffrey R. Carter
  1 sibling, 0 replies; 9+ messages in thread
From: Ludovic Brenta @ 2006-06-01 20:10 UTC (permalink / raw)


Schüle Daniel <uval@rz.uni-karlsruhe.de> writes:

> Hello,
>
> are there some page out there explaining Ada pointers for
> C programmers?
>
> Ada pitfalls for C mind set etc
>
> especially wheather or not object should explicitely
> allow to point to it
> how to create (heap) memory pools
> how to point to stack objects ... and so on

On http://www.adaic.org/learn/textbook.html , the book "Ada Distilled"
by Richard Riehle has what you want starting on page 33.

-- 
Ludovic Brenta.



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

* Re: pointers
  2006-06-01 19:46 pointers Schüle Daniel
  2006-06-01 20:10 ` pointers Ludovic Brenta
@ 2006-06-02  4:36 ` Jeffrey R. Carter
  1 sibling, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2006-06-02  4:36 UTC (permalink / raw)


Sch�le Daniel wrote:
> 
> are there some page out there explaining Ada pointers for
> C programmers?

The important thing for a C person to learn about Ada is not how to use 
access types, but how many things that cannot be done without pointers 
in C can be done without them in Ada. For most applications (language 
processing tools apparently excepted) you should never need to use 
access types in your code. The only time you'd use access types is to 
create dynamic data structures, and there only in the implementation, 
not the interface; since so many fine libraries already provide such 
structures and hide the access types, you'd only do that as a learning 
experience.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



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

end of thread, other threads:[~2006-06-02  4:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` Pointers David Kusuda
  -- 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

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