Path: utzoo!mnetor!uunet!husc6!uwvax!oddjob!gargoyle!ihnp4!twitch!anuck!jrl From: jrl@anuck.UUCP (j.r.lupien) Newsgroups: comp.arch Subject: Re: Null-terminated C strings Message-ID: <422@anuck.UUCP> Date: 22 Dec 87 20:37:49 GMT References: <261@ivory.SanDiego.NCR.COM> <164@sdeggo.UUCP> <174@quick.COM> <14116@think.UUCP> Organization: AT&T Bell Labs, Andover Ma. Lines: 53 Summary: good point, bad reasons In article <14116@think.UUCP>, barmar@think.COM (Barry Margolin) writes: > In article <174@quick.COM> srg@quick.COM (Spencer Garrett) writes: > >2) Having a CHARACTER to mark the end of a string is ever so much > >more convenient and efficient than having to compare lengths all the > >time > > The problem with this is that you must reserve a character. I've had This is a good point, but it is not a fatal problem. Note that whatever definition of strings you adopt, it is only relevant to string operations and string libraries. This in no way prevents the programmer from treating the strings they deal with as abstract data items, and writing their own library to manipulate it. > trouble with terminal emulators on machines whose keyboard-reading > system call returns 0 to indicate that no data is available, but 0 is > also the code for Control-@. As far as terminals are concerned, a null is a null. A driver that returns 0 on no data without setting ERRNO or something is broken, and must be fixed. > This type of problem occurs in file > reading, too. If you are reading a binary file, or perhaps a file of > keyboard operations (perhaps you recorded an Emacs session), as > characters the NULL character can no longer be used as a string > terminator; such files can contain any 8-bit code, so NO 8-bit > character can be used as a termination indicator. I have a program "strings(1)" which is able to distinguish null terminated strings in a binary file and print them out. This program is great for overcomming poor documentation and the like. The point is, you just check if the characters preceeding the null are printable. If not, they are not part of the string. > > Strings with lengths ALWAYS work. > No they don't. Did you read the article you responded to? Given a fixed format count (is it int? short? long?) there is a length of string you can't give the length of, due to overflow. Null terminated strings ALWAYS work in this regard. > Barry Margolin > Thinking Machines Corp. Barry, I am not trying to flame you. I would like to think that you have reasons for your attitude, but the ones given above don't hold water as stated. Would you mind elaborating? John Lupien Computing Tools ihnp4!mvuxa!anuxh!jrl