Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!uxc!uxc.cso.uiuc.edu!mcdurb!aglew From: aglew@mcdurb.Urbana.Gould.COM Newsgroups: comp.arch Subject: Re: String lengths Message-ID: <28200268@mcdurb> Date: 6 Feb 89 03:25:00 GMT References: <762@atanasoff.cs.iastate.edu> Lines: 51 Nf-ID: #R:atanasoff.cs.iastate.edu:762:mcdurb:28200268:000:2208 Nf-From: mcdurb.Urbana.Gould.COM!aglew Feb 5 21:25:00 1989 >>Since it was mentioned in the Endian Wars, does anyone know why C uses the >>null terminated string rather than an explicit length? >> - All machines I know of that have character string instructions want >> the length of the string. This forces the string primitives to first >> scan for null, a time wasting operation. > > I assume you mean something like: > > +------+---+---+---+---+---+---+---+---+---+---+---+---+---+ > |length| H | E | L | L | O | , | | W | O | R | L | D | \n| > +------+---+---+---+---+---+---+---+---+---+---+---+---+---+ > > Also, to quote K&R: "C was originally designed ... on the DEC PDP-11", > a machine with no string instructions. May I encourage people implementing string libraries to use an extra level of indirection? Instead of length immediately preceding the string, let length be associated with a pointer to the string. Makes substringing operations much easier, and has the ability to reduce unnecessary copies (at the risk of increased aliasing). +------+---+ |length|ptr| +------+---+ | +------+ | V +---+---+---+---+---+---+---+---+---+---+---+---+---+ | H | E | L | L | O | , | | W | O | R | L | D | \n| +---+---+---+---+---+---+---+---+---+---+---+---+---+ Machine instructions should not mandate the layout of strings in memory. They should, instead, require length and start to be preloaded in registers (where the machine'll have to put them anyway). That is, of course, if you *have* string instructions. As I am fond of pointing out, large integer operations can remove the need for many string operations (ie. give me a 64 or 128 bit wide bus, and a "STORE BYTES UNDER MASK" operation, and I don't *want* string operations). Andy "Krazy" Glew aglew@urbana.mcd.mot.com uunet!uiucdcs!mcdurb!aglew Motorola Microcomputer Division, Champaign-Urbana Design Center 1101 E. University, Urbana, Illinois 61801, USA. My opinions are my own, and are not the opinions of my employer, or any other organisation. I indicate my company only so that the reader may account for any possible bias I may have towards our products.