More exact.Is not exact. All supported string types derived from Ansi - AnsiString, UnicodeString etc - are zero terminated. Only exception are shortstrings (where Delphi also terminates these documented with a zero)
Pascal strings are not null terminated. They can contain nulls, because they have a length field.
More exact.NO. because the compiler treats s as ansistring and concatination is OK.
Pascal strings are not null terminated. They can contain nulls, because they have a length field.
But Pascal Strings (at least long/ansi, to which you can apply the mentioned "pchar(s)" cast) do have a terminating #0. Only that is not within the string. It is the next byte after the end of the string. The compiler automatically allocates the extra space for each string, and always puts the #0 in there.
I am not sure if that is
- documented behaviour
- implementation detail. (which means it could change with no notice)
In either case, the "pchar(s)" cast is documented. And it will continue to work.
Mind also that
var s: ansistrnig; p: pchar; begin p := pchar(s); s := s + 'abc'; // at this point p may become a dangling pointer. I.e. p may point to any kind of trash in memory.
Is not exact. All supported string types derived from Ansi - AnsiString, UnicodeString etc - are zero terminated. Only exception are shortstrings (where Delphi also terminates these documented with a zero)
What is going on behind the scene? Does the compiler automatically allocate a block of memory and copy the string to it and null-terminate it?What happens behind the scene depends on the string type and the string compiler options in effect.
More exact.Is not exact. All supported string types derived from Ansi - AnsiString, UnicodeString etc - are zero terminated.
Pascal strings are not null terminated. They can contain nulls, because they have a length field.
Mind also thatNO. because the compiler treats s as ansistring and concatination is OK.
var s: ansistrnig; p: pchar; begin p := pchar(s); s := s + 'abc'; // at this point p may become a dangling pointer. I.e. p may point to any kind of trash in memory.
I am not sure if that isFrom docs AnsiStrings (https://www.freepascal.org/docs-html/current/ref/refsu9.html#x32-380003.2.4): They are reference counted and are guaranteed to be null terminated.
- documented behaviour
I am not sure if that isFrom docs AnsiStrings (https://www.freepascal.org/docs-html/current/ref/refsu9.html#x32-380003.2.4): They are reference counted and are guaranteed to be null terminated.
- documented behaviour
Remark The null-termination does not mean that null characters (char(0) or #0) cannot be used: the null-termination is not used internally
I am not sure if that isFrom docs AnsiStrings (https://www.freepascal.org/docs-html/current/ref/refsu9.html#x32-380003.2.4): They are reference counted and are guaranteed to be null terminated.
- documented behaviour
More exact.Is not exact. All supported string types derived from Ansi - AnsiString, UnicodeString etc - are zero terminated. Only exception are shortstrings (where Delphi also terminates these documented with a zero)
Pascal strings are not null terminated. They can contain nulls, because they have a length field.
See my example.
All of this is documented in the manuals except the zero terminator for shortstring. Which is, as you rightly noted, not necessary but that goes for all the supported Pascal string types.