Path: utzoo!mnetor!uunet!husc6!rutgers!ucla-cs!zen!ucbvax!sdcsvax!ucsdhub!hp-sdd!hplabs!hpda!hpdslab!hpiacla!scottg From: scottg@hpiacla.HP.COM (Scott Gulland) Newsgroups: comp.arch Subject: Re: Re: Null-terminated C strings (design issues) Message-ID: <4900001@hpiacla.HP.COM> Date: 28 Dec 87 17:39:11 GMT References: <174@quick.COM> Organization: Hewlett Packard Lines: 45 / hpiacla:comp.arch / srg@quick.COM (Spencer Garrett) / 4:09 pm Dec 20, 1987 / In response to article <164@sdeggo.UUCP>, srg@quick.COM (Spencer Garret) writes: >I can think of two major advantages of null-terminated strings over >strings preceded by their lengths. > >1) You can pass substrings around without copying or alterning the >original string. (String tails, at least, but that's usually what >you want. "process some ... > >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 (assuming you're looking at the characters and not just copying >them, and even that is an implementation issue and not truly fundamental). >The general paradigm is: > > while (the next character is within the interesting range) > do something interesting with it; > now look at what the uninteresting character was; I would strongly disagree with the above statements. Operations such as concatenation and assignment are extremely inefficient on null-terminated strings when compared to strings which are preceeded by their lengths. This is because you must search through the string character by character to find the end of the string before you can perform the desired operation. I would also assert that string comparison, concatenation, assignment, etc occur much more frequently than the operations given in #1 above. Another benifit of strings preceded by thier length is that it is much easier to use strings in these compilers (less code to be written and a simplier syntax). This results in higher programmer productivity as well as string operation that perform at twice the levels of compilers with null-terminated strings. As far as the example given above, it is quite easy to do this without comparing length for non-null-terminated strings. For example: string[strlen(string)+1] = '\0' while (the next character is within the interesting range) do something interesting with it; now look at what the uninteresting character was; Note: In the PASCALs I have worked in, it is legal to insert characters past the current length of the string without affecting the strings length. ----------