Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!batcomputer!munnari.oz.au!metro!seagoon.newcastle.edu.au!cc.newcastle.edu.au!eepjm From: eepjm@cc.newcastle.edu.au Newsgroups: comp.lang.modula2 Subject: Re: How to write NEW? Message-ID: <1991Apr22.155056.10010@cc.newcastle.edu.au> Date: 22 Apr 91 05:50:56 GMT References: <5122672@janhh.hanse.de> Organization: University of Newcastle, AUSTRALIA Lines: 45 In article <5122672@janhh.hanse.de>, jan@janhh.hanse.de (Jan Willamowius) writes: > It looks like Wirth is using different ways to allocate memory > in different versions of his books. Sometimes he uses > 'Allocate(Var,SIZE(Type)' and sometimes 'NEW(Var)'. > > I like the second way better, but my compiler only supports Allocate. > So, is there a way to write a NEW (and DISPOSE, of course) ? > I wasn't going to respond to this at first, on the grounds that it's a common problem for beginners and that surely somebody else would quickly sort out Jan's problem ... then I watched in horror as a whole string of responses seemed to confuse the issue even more. Either a lot of people have misunderstood the question, or I have. The answer is actually quite simple, it's only the documentation that's bad. EVERY Modula-2 compiler supports NEW and DISPOSE (if you find one that doesn't, you've been cheated) in the following way. When NEW(Var) appears in your source, it is automatically translated by the compiler into Allocate(Var,SIZE(Var^)). The catch is - and this is where almost all first-time users of Modula-2 are trapped - that you have to remember to import Allocate from module Storage (or elsewhere). That is, you have to write something like: FROM Storage IMPORT Allocate, Deallocate; . . NEW(p); i.e. you have to call Allocate even though there is no explicit call to Allocate in your program. (By the way, some compilers spell it Allocate, and others use the upper-case ALLOCATE). NEW is not a procedure, even though the syntax makes it look like one. It's just a shorthand way of setting up a call to Allocate. By the way, you are perfectly free to re-write your own version of Allocate if you don't like the one supplied with your compiler. (I have done this myself, since the version of module Storage supplied with the FTL compiler doesn't have critical section protection, and crashes in multitasking applications). Hope this helps. Peter Moylan eepjm@cc.newcastle.edu.au