comp.lang.ada
 help / color / mirror / Atom feed
* Pointer types (I mean access types)
@ 2009-07-11  0:03 Rob Solomon
  2009-07-11  0:59 ` Adam Beneschan
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Rob Solomon @ 2009-07-11  0:03 UTC (permalink / raw)


I am trying to understand how pointers work in Ada.  I would like to
know if I have the correct equivalencies

Assume this declaration:
  type List_Node;  -- An incomplete type declaration.
  type List_Node_Access is access List_Node;
  type List_Node is
    record
      Data        : Integer;
      Next        : List_Node_Access;
    end record;

Ada                                               Modula-2 (or Pascal)
--------------------------------             ----------------------
type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;
Start : Node_Access;                      VAR Start : NodeAccess;
Current := new Node;                    Current := NEW(Node);
Current := Start;                            Current := Start;
Current.all := Start.all;                   Current^ := Start^;
Current.Data := 5;                         Current^.Data := 5;


I never learned C or derivatives.  So comparisons to C don't help me.

Thanks



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

* Re: Pointer types (I mean access types)
  2009-07-11  0:03 Pointer types (I mean access types) Rob Solomon
@ 2009-07-11  0:59 ` Adam Beneschan
  2009-07-11  1:23 ` Randy Brukardt
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Adam Beneschan @ 2009-07-11  0:59 UTC (permalink / raw)


On Jul 10, 5:03 pm, Rob Solomon <use...@drrob1-noreply.com> wrote:
> I am trying to understand how pointers work in Ada.  I would like to
> know if I have the correct equivalencies
>
> Assume this declaration:
>   type List_Node;  -- An incomplete type declaration.
>   type List_Node_Access is access List_Node;
>   type List_Node is
>     record
>       Data        : Integer;
>       Next        : List_Node_Access;
>     end record;
>
> Ada                                               Modula-2 (or Pascal)
> --------------------------------             ----------------------
> type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;
> Start : Node_Access;                      VAR Start : NodeAccess;
> Current := new Node;                    Current := NEW(Node);
> Current := Start;                            Current := Start;
> Current.all := Start.all;                   Current^ := Start^;
> Current.Data := 5;                         Current^.Data := 5;

I don't know Modula-2.  In Pascal, NEW is a procedure, not a function
that returns a value.  But you've got the Ada right.  You can also say
something like

   Current := new Node' (Data => 5, Next => null);

or

   Current := new Node' (Start.all);

to allocate a new Node and set up its value all at the same time.

Also, although .all is the equivalent to Pascal's ^, you can omit it
if it's followed by a record component or array index (or other
things).  So you could have said

   Current.all.Data := 5;

although nobody does.  It's equivalent to

   Current.Data := 5;

Hope this helps a little,

                               -- Adam



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

* Re: Pointer types (I mean access types)
  2009-07-11  0:03 Pointer types (I mean access types) Rob Solomon
  2009-07-11  0:59 ` Adam Beneschan
@ 2009-07-11  1:23 ` Randy Brukardt
  2009-07-11  9:01   ` Hibou57 (Yannick Duchêne)
  2009-07-11 19:41 ` anon
  2009-07-12  9:03 ` Gautier write-only
  3 siblings, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2009-07-11  1:23 UTC (permalink / raw)


"Rob Solomon" <usenet@drrob1-noreply.com> wrote in message 
news:t9lf55t55qh7puub368vnr55pf215d9nmh@4ax.com...
>I am trying to understand how pointers work in Ada.  I would like to
> know if I have the correct equivalencies
>
> Assume this declaration:
>  type List_Node;  -- An incomplete type declaration.
>  type List_Node_Access is access List_Node;
>  type List_Node is
>    record
>      Data        : Integer;
>      Next        : List_Node_Access;
>    end record;
>
> Ada                                               Modula-2 (or Pascal)
> --------------------------------             ----------------------
> type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;
> Start : Node_Access;                      VAR Start : NodeAccess;
> Current := new Node;                    Current := NEW(Node);
> Current := Start;                            Current := Start;
> Current.all := Start.all;                   Current^ := Start^;
> Current.Data := 5;                         Current^.Data := 5;
>
>
> I never learned C or derivatives.  So comparisons to C don't help me.
>
> Thanks

Looks right to me, although it has been so long since I used Pascal that I 
could have forgotten something.

                                    Randy.





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

* Re: Pointer types (I mean access types)
  2009-07-11  1:23 ` Randy Brukardt
@ 2009-07-11  9:01   ` Hibou57 (Yannick Duchêne)
  0 siblings, 0 replies; 6+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2009-07-11  9:01 UTC (permalink / raw)


On 11 juil, 03:23, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
> Looks right to me, although it has been so long since I used Pascal that I
> could have forgotten something.
>
>                                     Randy.

About “ New ”, I've just checked it in the FreePascal (also previously
known as FPC or FPK) reference.

It is indeed declared as:

> procedure New (var P: Pointer);
> procedure New(var P: Pointer; Cons: TProcedure);

The second version is to be used with a constructor.

(I had forgot it, I didn't use to use it a lot)

But the form TObject.Create, which is a function, is perhaps more
common now (at least to me).


On 11 juil, 02:59, Adam Beneschan <a...@irvine.com> wrote:
>    Current.all.Data := 5;
>
> although nobody does.  It's equivalent to
>
>    Current.Data := 5;
>
> Hope this helps a little,
>
>                                -- Adam

The implicit dereference avoid to have to rewrite source when a
compononent or a variable switch from a reference to an instance or
vice-versa.

On 11 juil, 02:59, Adam Beneschan <a...@irvine.com> wrote:
> I don't know Modula-2.
An attempt to get the best of Pascal while solving its fallbacks (a
bit like Ada did). There was also Oberon in this place, which in
turned tried to solve some of the fallbacks of Modula. Oberon was a
project at ETHZ (Eidgenössische Technische Hochschule Zürich).... just
like Eiffel was ;) (lot of good things in the same place)

On 11 juil, 02:03, Rob Solomon <use...@drrob1-noreply.com> wrote:
>[...]
> type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;
>
> [...]
> Thanks

You also have an easy goody with Ada 2005 : the null exclusion.

This allow you to tell that a given pointer type (which may be an
anonymous type, i.e. a type without a name) must never be null.

You may do it like this:
> type Node_Access is not null access Node;

But becareful while declaring a variable of this type : this will have
to be obviously initialize.

If you want to restrict access to read only, use “ type Node_Access is
access constant Node ”.

Just like as with Pascal, you may also have “ pointers ” to procedures
in Ada. To get it, just do “ type Callback_Procedure is not null
access procedure (...); ”

A less easy struff: one more aspect you will learn later (take the
time you need) is storage pool. You may sometime got compilation error
suggesting you to add the “ all ” access modifier, to get something “
type Node_Access is access all Node ”. This “ all ”, which is not the
same which is intended for explicite dereference, means that your
access type is intended to access data stored in any sorage pool.




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

* Re: Pointer types (I mean access types)
  2009-07-11  0:03 Pointer types (I mean access types) Rob Solomon
  2009-07-11  0:59 ` Adam Beneschan
  2009-07-11  1:23 ` Randy Brukardt
@ 2009-07-11 19:41 ` anon
  2009-07-12  9:03 ` Gautier write-only
  3 siblings, 0 replies; 6+ messages in thread
From: anon @ 2009-07-11 19:41 UTC (permalink / raw)


Most books on both languages have the link link example. just find one 
for each lang and compare. Execpt for the deallocation routine, your 
list look correct.

In Ada, for deallocation, there is a package "Ada.Unchecked_Deallocation" that 
is used to create a Free or Delete routinue for each type. An example taken 
from Ada.Text_IO is:

  -- Text_AFCB is a predefined record.

   procedure AFCB_Free (File : access Text_AFCB) is
      type FCB_Ptr is access all Text_AFCB;
      FT : FCB_Ptr := FCB_Ptr (File);

      procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);

   begin
      Free (FT);
   end AFCB_Free;



In <t9lf55t55qh7puub368vnr55pf215d9nmh@4ax.com>, Rob Solomon <usenet@drrob1-noreply.com> writes:
>I am trying to understand how pointers work in Ada.  I would like to
>know if I have the correct equivalencies
>
>Assume this declaration:
>  type List_Node;  -- An incomplete type declaration.
>  type List_Node_Access is access List_Node;
>  type List_Node is
>    record
>      Data        : Integer;
>      Next        : List_Node_Access;
>    end record;
>
>Ada                                               Modula-2 (or Pascal)
>--------------------------------             ----------------------
>type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;
>Start : Node_Access;                      VAR Start : NodeAccess;
>Current := new Node;                    Current := NEW(Node);
>Current := Start;                            Current := Start;
>Current.all := Start.all;                   Current^ := Start^;
>Current.Data := 5;                         Current^.Data := 5;
>
>
>I never learned C or derivatives.  So comparisons to C don't help me.
>
>Thanks




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

* Re: Pointer types (I mean access types)
  2009-07-11  0:03 Pointer types (I mean access types) Rob Solomon
                   ` (2 preceding siblings ...)
  2009-07-11 19:41 ` anon
@ 2009-07-12  9:03 ` Gautier write-only
  3 siblings, 0 replies; 6+ messages in thread
From: Gautier write-only @ 2009-07-12  9:03 UTC (permalink / raw)


On Jul 11, 2:03 am, Rob Solomon <use...@drrob1-noreply.com> wrote:
> I am trying to understand how pointers work in Ada.  I would like to
> know if I have the correct equivalencies
>
> Assume this declaration:
>   type List_Node;  -- An incomplete type declaration.
>   type List_Node_Access is access List_Node;
>   type List_Node is
>     record
>       Data        : Integer;
>       Next        : List_Node_Access;
>     end record;
>
> Ada                                               Modula-2 (or Pascal)
> --------------------------------             ----------------------
> type Node_Access is access Node; TYPE NodeAccess = POINTER TO Node;

Would be type NodeAcces = ^Node in Pascal IIRC ...

> Start : Node_Access;                      VAR Start : NodeAccess;
> Current := new Node;                    Current := NEW(Node);
> Current := Start;                            Current := Start;
> Current.all := Start.all;                   Current^ := Start^;
> Current.Data := 5;                         Current^.Data := 5;

Seems fine to me (I had a course in Modula-2; never had in Ada :-) ).
Note that Ada's "Current.Data := 5" is a shortcut for
"Current.all.Data := 5"

G.



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

end of thread, other threads:[~2009-07-12  9:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-11  0:03 Pointer types (I mean access types) Rob Solomon
2009-07-11  0:59 ` Adam Beneschan
2009-07-11  1:23 ` Randy Brukardt
2009-07-11  9:01   ` Hibou57 (Yannick Duchêne)
2009-07-11 19:41 ` anon
2009-07-12  9:03 ` Gautier write-only

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