201: range check
s:= ''; ... s[1] // Yes that is a range check
The #0 is not within range.
Also the empty string, technically is a nil pointer. So there is no #0.
...
Ok need to correct myself.
According to your code, fpc does the extra work. So then yes, pchar is save in that case.
BlockWrite(F, Pchar(S)^, length(S));
For long strings, this is not the case. PChar(s) in this case is a function that checks for nil, and if so, changes result to a pointer to a memory area with #0.P will be nil too. There is no memory for s. THere is nowhere to point to.
s := ''; p := Pchar(s)
For long strings, this is not the case. PChar(s) in this case is a function that checks for nil, and if so, changes result to a pointer to a memory area with #0.P will be nil too. There is no memory for s. THere is nowhere to point to.
s := ''; p := Pchar(s)
It's not a function, neither in the language sense nor in the sense of the compiler. The compiler simply does a more complex type conversion.It doesn't matter what you call it. In Delphi, these are intrinsic routines like Length or Abs. In FPC it's complex type conversation.
It is a pity that unlike Delphi (http://docwiki.embarcadero.com/RADStudio/Sydney/en/String_Types_(Delphi)#Mixing_Delphi_Strings_and_Null-Terminated_Strings), this is not documented in FPC.
How about you file a bug against the documentation then?
The internal representation as a pointer, and the automatic null-termination make it possible to typecast an ansistring to a pchar. If the string is empty (so the pointer is Nil) then the compiler makes sure that the typecast pchar will point to a null byte.and from Multi-byte String types (https://www.freepascal.org/docs-html/current/ref/refsu10.html#x33-420003.2.5)
Unicodestrings (used to represent unicode character strings) are implemented in much the same way as ansistrings: reference counted, null-terminated arrays, only they are implemented as arrays of WideChars instead of regular Chars.
...
The Widestring type (used to represent unicode character strings in COM applications) is implemented in much the same way as Unicodestring on Windows, and on other platforms, they are simply the same type.