by jhallenworld · 1w ago · 0 comments
Depends on what you mean by general purpose I guess, but I remember I liked it because of C strings. It's difficult to understand these days, C's strings were better than those in other languages: no 255 byte length limit, 0-based instead of 1-based, escape sequences so that you could have any byte in a string (Turbo Pascal supported control characters, but PASCAL in general did not). Other languages at the time really only supported fixed length space filled strings.
Another big thing was existence of malloc().
Null-terminated requires that you have sentinel (null) at the end, instead of a length at the beginning.
Having a length (encoded as a byte at the beginning), means they are very easy and efficient to copy and concatenate, because unlike the null-terminated variant, you don't have to search for the terminator which requires a conditional branch on each character until the length.
Conditional branches were really expensive on old CPUs, compared to the 8086's rep movsb instruction, that could just copy a specific length of bytes. On the other hand, I suppose, other string-operations are more tricky, because you need to keep track of your position in the string as you iterate over it.
I've never thought null-terminated strings were a particularly neat design or easy to work with (there are pros and cons) -- But the 255 string limit was annoying, and I wished they would have had pascal strings up-to 64K in length (they could have used a word as length instead of a byte).
Malloc is by no means a 'C' specific thing -- Turbo Pascal had getmem() to allocate memory on the heap.
Turbo Pascal 1.5 for Windows 3.1 introduced C strings (PChar), and afterwards, Turbo Pascal 7 took open arrays from ISO Extended Pascal.
Malloc() is a primitive library call where one always has to do sizeof math for memory allocation, if you wanted that fun in Turbo Pascal instead of plain New, you could use GetMem and FreeMem functions.
And the concept of areas was already supported with Mark/Release.