← Back to context

Comment by Conscat

16 days ago

A Pascal string has a leading length byte. Because that is one byte, the text can't exceed 255 characters.

For modern hardware, a 64-bit length is more practical though - no alignment issues. It seems to me that Pascal's specifying a single byte prefix was a language design "mistake" of the same type as NULL termination, putting hardware considerations into the language definition. Very practical for machines of the time, but not necessarily the best choice in hindsight.

  • The original Pascal didn't had a string type, that was introduced by various dialects.

    FWIW all Pascal dialects since the 90s have a string type that allows more than 255 bytes. In Free Pascal strings are pointers to the first character with a header in a negative offset indicating the length, reference count and codepage (these fields are aligned depending on the CPU). For C compatibility the string is also null terminated so you can pass such a string to a C function and it'll work as expected. AFAIK Delphi also does the same.

  • > single byte prefix was a language design "mistake"

    Easy to say in hindsight.

    It was an optimisation made back when every single byte mattered because you might have some kilobytes of memory and a 6502 CPU (where you strongly avoided using 16 bit pointers or arithmetic - because your program would be too bloated otherwise).

    At the time Pascal was used, a whole byte for each string was seen as a waste - so fixed length strings were often used instead.