Path: utzoo!attcan!uunet!cs.utexas.edu!sun-barr!apple!bloom-beacon!usc!orion.cf.uci.edu!uci-ics!milne From: milne@ics.uci.edu (Alastair Milne) Newsgroups: comp.lang.pascal Subject: Re: Pointers.. Keywords: pointers traversal dispose Message-ID: <16404@paris.ics.uci.edu> Date: 2 Jun 89 01:29:00 GMT References: <3398@westfort.UUCP> <8835@phoenix.Princeton.EDU> <16969@swrinde.nde.swri.edu> <1150@draken.nada.kth.se> Sender: news@paris.ics.uci.edu Reply-To: Alastair Milne Organization: Educational Technology Center, Dept. of ICS, UC Irvine Lines: 33 d87-jse@nada.kth.se (Joakim Sernbrant) writes >Talking about pointers, is there any reason why I shouldn't do this on >a PC running DOS without multitasking: > >while p <> nil do begin > dispose(p); > p := p^.next; >end; Problems here start well before multitasking: you are accessing part of a variable (p^.next) after having destroyed it (dispose(p);). Now it may be that your implementation of dispose doesn't actually impede your doing this (some Pascal's, for instance, have dispose return the pointer passed to them set to NIL), but I certainly wouldn't rely on it -- at best, it would be very untransportable. What I think you actually want is: while p <> nil do begin holder := p; p := p^.next; Dispose( holder); end; I don't know nearly enough of how OS/2 handles task-switching (time-slicing? priority queues?) to know what operations are guarded and what aren't; but I think this algorithm should dispose of a list with no incoherence problems. At most, some concurrent application might be grabbing the memory this one releases as it is released -- but that should cause no problems. Alastair Milne