Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!mel.dit.csiro.au!latcs1!stephens From: stephens@latcs1.oz.au (Philip J Stephens) Newsgroups: comp.sys.apple2 Subject: Re: new() procedure in Pascal Message-ID: <8744@latcs1.oz.au> Date: 11 Sep 90 10:19:48 GMT References: <5429.26ec0e29@vax5.cit.cornell.edu> Distribution: comp Organization: Comp Sci, La Trobe Uni, Australia Lines: 45 Joel Sumner writes: > One more Pascal related question. When should I call the following? > > var p:pointer (or StringPtr, or anything pointer) > begin > .... > new(p); > .... > end; > > In other words, when do you have to call new()? A pointer variable is simply that: a two-byte variable which contains an address to the actual data (thus the value of p is an address, and the value of p^ is the data stored at that address). Initially, p is not pointing to anything at all, hence referencing p^ is likely to crash the program. The procedure new(p) allocates some memory to hold the data (whose type is given in the var definition), and stores the address of that data in p. The following program will thus allocate an integer variable to the pointer p, store the value 10 in the integer variable pointed to by p, then write that value out. var p: ^integer; begin new(p); p^ := 10; writeln(p^); end; I don't think you can use new(p) if p is of type pointer, as you are not specifying what TYPE of data p must point to. I hope that helps. < Philip J. Stephens >< "Many views yield the truth." > < Hons. student, Computer Science >< "Therefore, be not alone." > < La Trobe University, Melbourne >< - Prime Song of the viggies, from > < AUSTRALIA >< THE ENGIMA SCORE by Sheri S Tepper > <\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/><\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/>