Article View: comp.lang.oberon
Article #854Re: NEW(p)
From: Eric W. Nikitin
Date: Sat, 27 Jan 1996 06:01
Date: Sat, 27 Jan 1996 06:01
122 lines
5315 bytes
5315 bytes
Bronxx (rmichl@iiic.ethz.ch) wrote: : 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) : Allright, let's start with what I know: : The declaration of a pointer does not produce the variable it points to. : We have to generate the variable explicitly by NEW(p), where the actual : parameter p is a pointer of type P. NEW(p) creates a variable that is : an instance of the base type of P. The pointer p is initialized such that : it points to that instance. : That means we have to create each pointer with NEW(p) before we can : work with it. : For example : TYPE P = POINTER TO PDesc; : PDesc = RECORD key: Key; next: P END; : VAR p:P; : What about the p.next then? It is a pointer too, does it have to : be created with NEW() as well? I guess not. What is it's value? NIL? Yes, its value is NIL. : Before we can assign something to p or p.next, we have to create this : with NEW(), right? Yes, at some point in program execution, a NEW() operation must be done in order for something to be assigned to p.next. : Because there is no such thing as a "spontaneous pointer", it doesn't : exist before we create it with NEW(). NEW() doesn't create the pointer... NEW() creates the thing the pointer points at :) : Therefore we have to produce each node (e.g. of a list) with NEW(node), : before we add it to the list. : My problem seems to be too many NEW instruction (because I think we have to create every pointer). Please help me here: As in my reply to your previous post, lists are dynamic structures. That is, their size changes as you manipulate them. NEW() is required because otherwise you'd have to allocate all your nodes at the start of the program. NEW() creates new nodes and the automatic garbage collection removes the nodes when they are no longer needed. I really can't think of any way around this... the programmer has to decide when nodes are created and how many to create. Even the programming language Smalltalk has a NEW operator... : (Let "first:P" be a pointer that provides a link to the first list node.) : An example of a "removal of the first node of a list" is: : PROCEDURE FirstNode(VAR first: P):P; : VAR n: P; : BEGIN : n := first; : IF n # NIL THEN first := n.next END; : RETURN n : END FirstNode; : My questions now: : * Why can we assign something to n without a NEW(n) statement? : * Why is there no NEW(n) at all? This is another instance where I don't like the 'style' of the examples. To the novice, this example is fairly cryptic... though to anyone who has used pointers in other languages, it's pretty straightforward. The example intends to illustrate an operation on a list. But nowhere in the example is it obvious that you're working on a list. Lists can be programmed in a number of different ways. The more precise term for the list in the example is a "linked list"... because each member of the list is linked to the next member of the list. There is no explicit way for you to operate on the entire list at once. You need to look at the first element on the list... which leads to the second... and then the third... and so on... The function "FirstNode" intends to take the first element off of the list and return it. As a 'side-effect' the list itself is changed so that what was the second element has now become the first element. "first" is actually two things at once 1) the list you want to operate on... and 2) the first element of the list. It is assumed that somewhere along the line you have added nodes to the list "first"... say in a PROCEDURE named AddNode. It is in AddNode that you would have used NEW(). "FirstNode" is passed a list of nodes designated by "first". "n" is used as a 'temporary variable'. The first assignment 'n := first;' means 'make "n" point at the first element in the list'. "n" is then checked to make sure it actually points at something (i.e. it does not point at NIL - which would happen if you passed an empty list to the function). If "n" points to a valid node, then "first" is made to point at the next element on the list (which, by the way, could in fact be NIL). "n" still points at what was originally the first element on the list and the function returns that first element. The list designated by "first" is now actually the rest of the list with its first element removed. : I assume because we did VAR n:P and first exists as a variable : parameter. I hope this helps :) Eric
Technical Details
Message-ID:
<4ecf43$93h@wariat.wariat.org>Path:
arc1-ca.pugleaf.net!junior.wariat.org!enikitinReferences:
<4eak7d$nm5@neptune.ethz.ch>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!junior.wariat.org!enikitin From: enikitin@junior.wariat.org (Eric W. Nikitin) Newsgroups: comp.lang.oberon Subject: Re: NEW(p) Date: 27 Jan 1996 06:01:39 GMT Organization: Akademia Pana Kleksa, Public Access Uni* Site Message-ID: <4ecf43$93h@wariat.wariat.org> References: <4eak7d$nm5@neptune.ethz.ch> NNTP-Posting-Host: junior.wariat.org X-Newsreader: TIN [version 1.2 PL2]