Your code is technically invalid. It only works because of implementation details you cannot rely on.
Let's look at the corresponding section of code:
P := PChar(MyArray[0]); MyArray[0] := MyArray[1]; MyArray[1] := P;
First we make P the point of the first character MyArray [0]. Then we assign MyArray [0]. There is currently no reason for the string buffer at which point P remains alive. No string variable refers to it. His countdown has gone to zero, and therefore he must be freed. This makes subsequent use of P invalid.
In this case, why does your code work? Because the strings you use are literals. And therefore, they are stored with a number of links equal to -1, and bypass the usual heap allocation routines used by strings. But if you used string values ββthat were not literals, then what I described in the paragraph above would have come true. And I expect your real code does not use literals.
So, your actual question is somewhat controversial. Pointer P simply points to a block of memory. It remains to point to the same block of memory until you change the pointer. If you change the contents of the memory block, then P will see these changes if you cancel the link to it. It is just a pointer like any other pointer.
You need to take care using the PChar variable. In your use, this is an unmanaged pointer to an object controlled by the compiler. This gives you plenty of room for error, and you are trapped. If you need a copy of a string, take a copy to another string variable.
source share