Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!snorkelwacker.mit.edu!mit-eddie!uw-beaver!zephyr.ens.tek.com!vice!bobb From: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Newsgroups: comp.lang.pascal Subject: Re: string ghosts Message-ID: <6992@vice.ICO.TEK.COM> Date: 28 Feb 91 00:45:32 GMT References: <26159@adm.brl.mil> Reply-To: bobb@vice.ICO.TEK.COM (Bob Beauchaine) Organization: Tektronix, Inc., Beaverton, OR. Lines: 43 In article <26159@adm.brl.mil> STANKULI%UWF.BITNET@cunyvm.cuny.edu ( stan kulikowski ii) writes: >(* > i was having some strange behavior in one of my TP 5.5 programs... > a string variable was behaving as if it still had the contents of previous > assignments after it had been nulled. even the turbo debugger saw > that the string had null value, but it still operated as if the ghost > of its previous assignment was still there... >*) [example code deleted] Yes, your understanding of the way strings work in Turbo Pascal is incorrect. Nulling a string in Turbo Pascal only sets the length byte (byte 0) to the null character. Any information previously contained in the string is preserved. If you truly want to null the entire string, you would have to manually set each byte to #0 through an index or a call to fillchar(). (BTW, I verified the behavior of strings with your example code). >(* > is this the way pascal is supposed to run? > > if so, this is very counterintuitive. when i null out a string, > i obviously intend that all of its contents should be likewise null. > surely any reference to a zero-length null string returns nil value. > Depends on what's more important to you. Turbo seems to value speed above all else in programs, and simply nulling the length byte of a string is far superior than nulling each element from an efficiency standpoint. It also allows you to do things like turn off var string checking (compiler option $V) and manipulate strings whose maximum length may not be known at compile time. Nulling every element of a 50 character string when a 10 character length string was passed as a parameter would be disastrous. The moral of the story is : don't be stupid. If the length of your string is zero, any reference to any element of that string is at best undefined. /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Bob Beauchaine bobb@vice.ICO.TEK.COM C: The language that combines the power of assembly language with the flexibility of assembly language.