But once you change the string it will be made unique. This is the Copy-On-Write mechanism of managed strings.
So I wonder why do you need to copy the whole string on writing a single byte ?Because "copy-on-write mechanism". If two different strings refer to the same memory buffer and one of them is modified, the other must remain unchanged, so the data must be copied.
So I wonder why do you need to copy the whole string on writing a single byte ?
The copy-on-write mechanism is for passing by value parameters to chains of methods. Multiple methods might only pass it on, but don't need a deep copy, only ref count increase.
If some method starts modifying though, if you wouldn't copy, after the method is finished, and the calling methods continue they would see the modified string, not the original, despite passing by value.
So the whole idea is lazy by-value semantics.
Also it means that a pchar pointing into a string is only safe as long as the string remains in scope.
Pascal strings always end with a NUL to simplify conversion to PChar.
Please note that this applies to AnsiString, WideString and UnicodeString, but not to ShortString.
If an instance of c is not found, the results are unpredictable.Thus when surpassing the NULL Byte without checking it the PChar would just walk on and on throughout the whole Process Address Space