Article View: comp.lang.oberon
Article #865Re: NEW(p)
From: ARCJ7
Date: Wed, 31 Jan 1996 07:41
Date: Wed, 31 Jan 1996 07:41
55 lines
2575 bytes
2575 bytes
In article <4eak7d$nm5@neptune.ethz.ch>, rmichl@iiic.ethz.ch says...
>
>NEW(p)
>~~~~~~
>
>I have problems with this creation of pointers (as I mentioned in
>an earlier post, this is because of many errors in the book about
>Oberon by M. Reiser and N. Wirth, even if it is a very good book,
>I'm disappointed about the chapter "pointers").
>
>But basically, a pointer doesn't exist if it's not created with
>a NEW(p) (p:P is declared by VAR or TYPE as a POINTER TO basetype)
>
I've had the same problem of interpretation when learning Pascal (ages ago);
the syntax is the same so the interpretation problem remains in Oberon.
What happens is that the declaration
VAR p : POINTER TO some type T;
does create a variable p of type POINTER. That is, a (likely 32-bits) entity
capable of containing an address or reference to any type (forced by the
compiler to contain addresses to instantiations of type T only).
It is the procedure call
NEW(p);
that creates a (nameless) instantiation of the type T, implied by the
declaration of p as referencing things of type T, and that lets p point
to this instantiation:
NEW(p) == (1) create record of type T somewhere in memory;
(2) p := address of record.
In this respect, the C-like syntax
p := NEW(T);
would indicate better what happens when calling NEW() and would allow
error recovery (NEW(T) = NIL => no more room for dynamic variables).
The fact that a VAR declaration does create room for a POINTER type also
explains why you do not need to call NEW() for every pointer variable.
Because you can assign the address of nameless instantiations of T
repeatedly to other pointer variables, you can manipulate these
instantiations without explicitly referring to them.
Note that here lies the danger too: precisely because these instantiations
are nameless and can't be explicitly identified, it is easy to 'lose track'
of them!
If you want to study more examples, you can always take a look at books
on Pascal or Modula-2. Except for type extensibility, these should contain
valuable sections on pointers.
Hope this helps..
-Henryk.
Technical Details
Message-ID:
<4en6em$jul@alley.fel.tno.nl>Path:
arc1-ca.pugleaf.net!news.nic.surfnet.nl!sun4nl!alley!usenetReferences:
<4eak7d$nm5@neptune.ethz.ch>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.nic.surfnet.nl!sun4nl!alley!usenet From: arciszewski@fel.tno.nl (ARCJ7) Newsgroups: comp.lang.oberon Subject: Re: NEW(p) Date: 31 Jan 1996 07:41:10 GMT Organization: TNO-FEL Message-ID: <4en6em$jul@alley.fel.tno.nl> References: <4eak7d$nm5@neptune.ethz.ch> NNTP-Posting-Host: 134.203.13.50 Mime-Version: 1.0 Content-Type: Text/Plain; charset=ISO-8859-1 Keywords: pointer X-Newsreader: WinVN 0.99.5