• Sun. Aug 14th, 2022

shoosh infosite

s….s INFO

Null-terminated string


Dec 17, 2021

In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article). Alternative names are C string, which refers to the C programming language and ASCIIZ[citation needed] (although C can use encodings other than ASCII).

The length of a string is found by searching for the (first) NUL. This can be slow as it takes O(n) (linear time) with respect to the string length. It also means that a string cannot contain a NUL (there is a NUL in memory, but it is after the last character, not “in” the string).

. . . Null-terminated string . . .

Null-terminated strings were produced by the .ASCIZ directive of the PDP-11assembly languages and the ASCIZ directive of the MACRO-10 macro assembly language for the PDP-10. These predate the development of the C programming language, but other forms of strings were often used.

At the time C (and the languages that it was derived from) was developed, memory was extremely limited, so using only one byte of overhead to store the length of a string was attractive. The only popular alternative at that time, usually called a “Pascal string” (a more modern term is “length-prefixed“), used a leading byte to store the length of the string. This allows the string to contain NUL and made finding the length of an already stored string, need only one memory access (O(1) (constant) time), but limited string length to 255 characters (on a machine using 8-bit bytes). C designer Dennis Ritchie chose to follow the convention of null-termination to avoid the limitation on the length of a string and because maintaining the count seemed, in his experience, less convenient than using a terminator.[1][2]

This had some influence on CPU instruction set design. Some CPUs in the 1970s and 1980s, such as the Zilog Z80 and the DECVAX, had dedicated instructions for handling length-prefixed strings. However, as the null-terminated string gained traction, CPU designers began to take it into account, as seen for example in IBM’s decision to add the “Logical String Assist” instructions to the ES/9000 520 in 1992 and the vector string instructions to the IBM z13 in 2015.[3]

FreeBSD developer Poul-Henning Kamp, writing in ACM Queue, referred to the victory of null-terminated strings over a 2-byte (not one-byte) length as “the most expensive one-byte mistake” ever.[4]

. . . Null-terminated string . . .

This article is issued from web site Wikipedia. The original article may be a bit shortened or modified. Some links may have been modified. The text is licensed under “Creative Commons – Attribution – Sharealike” [1] and some of the text can also be licensed under the terms of the “GNU Free Documentation License” [2]. Additional terms may apply for the media files. By using this site, you agree to our Legal pages . Web links: [1] [2]

. . . Null-terminated string . . .