Article View: comp.lang.oberon
Article #853Re: Help! POINTERS
From: Eric W. Nikitin
Date: Sat, 27 Jan 1996 05:57
Date: Sat, 27 Jan 1996 05:57
157 lines
7143 bytes
7143 bytes
I am not sure of what level of programming experience you have, so please forgive me if I seem to be 'talking down' to you :) Bronxx (rmichl@iiic.ethz.ch) wrote: : Someone please confirm the heaps of errors in the book : "Programming in Oberon" <Martin Reiser, Niklaus Wirth>. : (I'm referring to the 1992 edition and don't know if there : is a newer one.) : It's a very important section in the book (pointers), and I : think it's terrible that one can't rely on the creator's manual : on such a crucial topic. : So first we look at page 135, where the authors introduce a : Pointer declaration: : p.135: TYPE : ListNode = POINTER TO ListNodeDesc; : ListNodeDesc = RECORD : key: Key; next: Node (*) : END; : VAR first: ListNode; : (*) The field named next should obviously be of pointer type : "ListNode", not "Node"! It looks like an error to me as well. Though it is not a "serious" error, it would be confusing to someone trying to teach the language to themselves. : On the next page, some other examples are given, like TreeNode etc. : (TreeNode = POINTER TO TreeNodeDesc: TreeNodeDesc = RECORD ...) I'm sorry... was there something wrong with these examples? : On page 137: : I must admit, that I have some problems understanding the NEW(p) : command, not least because of this book, but I think there are : errors here too: : p.137: For example, assume first: ListNode and root: TreeNode. Then : NEW(first) A variable of type ListNode (*) is created, : which is designated by pointer first. : NEW(root) A variable of type TreeNode (**) is created, : which is designated by pointer root. : Okay now, I suppose TreeNode and ListNode are of pointer type. : So first and root are declared as pointers. : Shouldn't it be "ListNodeDesc" instead of "ListNode" at position : marked with (*) and similarly "TreeNodeDesc" at (**) ? NEW() is essential for creation of dynamic data structures. Otherwise, you'd be limited to using static structures like ARRAYs. The beauty of structures like lists is that they can grow to be as large as you need them to be (based on hardware constraints of course :) Now, as for what it means when you use NEW(). Let's take your first example: NEW(first). We are assuming that you've made a declaration first: ListNode; before calling NEW(first). This declaration creates a variable of type ListNode (i.e. POINTER TO ListNodeDesc). The variable is initialized to NIL and there does not yet exist anything in computer memory for this pointer to point at. That is where NEW() comes in. NEW(first) allocates memory (i.e. reserves space in memory for the values that ListNodeDesc is supposed to hold) and 'points' the variable "first" at the memory that has been allocated. So to answer your question, IMO it would be more correct to say NEW(first) creates a variable of type ListNodeDesc, which is accessed by the pointer "first". *HOWEVER*, the TYPE of the variable "first" is ListNode *not* ListNodeDesc. I think this was the intent of the statements in PIO... to give emphasis on they TYPE of "first". : At the next page, I still have the declaration of type : ListNode = POINTER and ListNodeDesc = RECORD in my head (as above), : because nowhere is mentioned that the declaration changes at this : point. But I think for the chapter "Implied dereferencing", the : authors have chosen a different type declaration, without telling : us so! Because: : p.138: first.next stands for first^.next : [...] : Thus if a field or index selector is present we may use the : pointer as if it were a name of the variable pointed at. We : stress, however, that this abreviation holds only for fields : and array elements. The designator first , for example, de- : notes a pointer value, not an object of type ListNode. (*) : Such an object is referenced by first^ . To refine this : point, let node: ListNode (**) and consider the assignments: : node := first illegal assignment, pointer to record : node := first^ legal assignment, record to record. : I understand the dereferencing principle, but still I think the types : are wrong here again! Because in (*), "ListNode" obviously isn't a : pointer anymore. (My proposition would be "ListNodeDesc"), and I think : Martin Reiser and Niklaus Wirth want to define in (**) that node is : of record type. (But according to earlier definitions, this should be : "node: ListNodeDesc"). : (**) Until now "ListNode" was a pointer (p. 135), but now "ListNode" : is a record. : Therefore I assume the type declaration has changed from the one on : page 135 to this one: : TYPE Node = POINTER TO ListNode; : ListNode = RECORD : key: Key; : next: Node : END; : I complain of course because noone tells this anywhere, and it is : confusing! [On page 140, a new pointer will be declared, like "Node" : and "NodeDesc", but this isn't exactly what we need here, and anyway, : I tend to read my books from low page numbers to higher ones, and in : that order :)] I entirely agree with you here. Those of us with experiences with pointers from other languages can certainly figure this stuff out, but it is confusing and a novice programmer would certainly have a rough time with it. : On the very next page, we see a picture of a sample list, composed : of three nodes, and here "first: ListNode" is clearly a pointer again! : Please tell me if I'm wrong. I want to understand these things, but how : can I if errors and unmentioned assumptions stop me? I must admit, I was also somewhat disappointed when I first got PIO. Besides the types of errors you have pointed out, there is a decided lack of consistency of style. I could, of course, write Oberon programs that look very much like the Pascal I learned many years ago. But to take full advantage of Oberon's (and Oberon-2's) extensibility, you must program in a different style. There are definitely fewer books and other resources for those wanting to learn Oberon than for more 'popular' languages like C++. It is a shame that PIO isn't as good a resource as it could be. Perhaps 'Object Oriented Programming in Oberon-2' is better... but I haven't read it so I can't say for certain. : Thank you, : Robert : In my next posting I'll have a question concerning NEW(p) again. : %% rmichl@cinenet.ch %% http://www.amiga.icu.net.ch/~rmichl %% : %% Robert Michl %% Fabrikstrasse 21 %% 8005 Zuerich %% CH %%
Technical Details
Message-ID:
<4ecerq$93h@wariat.wariat.org>Path:
arc1-ca.pugleaf.net!junior.wariat.org!enikitinReferences:
<4eagcg$l6d@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: Help! POINTERS Date: 27 Jan 1996 05:57:14 GMT Organization: Akademia Pana Kleksa, Public Access Uni* Site Message-ID: <4ecerq$93h@wariat.wariat.org> References: <4eagcg$l6d@neptune.ethz.ch> NNTP-Posting-Host: junior.wariat.org X-Newsreader: TIN [version 1.2 PL2]