UniqueString() will give you a completely independent copy.
https://www.freepascal.org/docs-html/rtl/system/uniquestring.html
{$mode objfpc}{$H+} var A, B: UnicodeString; begin A := 'Hello'; B := A; UniqueString(B); A := 'Pony'; A := 'Gravy'; writeln(A); // prints Gravy writeln(B); // prints Hello end.
8
I just spend the past 3 hours trying to debug my program... I thought I was going mad. This was the most infuriating bug I have EVER ran into. Wow... – 43.52.4D. Mar 17 '16 at 7:05
12
@43.52.4D. Only it's not a bug :-) – yuvi Mar 17 '16 at 7:13
B := copy(A, 1, Length(A));
?
Well, now that I've gotten some sleep I'm gonna take my code back to what it was when I first encountered this problem - a two dimensional expanding TVector of TVector. I read somewhere that push_back in std::vector called the copy constructor. So I made what I thought was a "unique" copy constructor. But at some point I became completely confused and some of what I reported may not have been accurate, i.e, the element-by-element copying might have worked after all. At any rate, I'm unwinding all my changes and starting over with a known solution. This whole thing reminded me of this:Why are you talking about C++ related rules in a Pascal related thread? Pascal does not have copy constructors or such things, so even if we have a TVector that's inspired by the STL it does not behave the same way.
https://stackoverflow.com/questions/19951816/python-changes-to-my-copy-variable-affect-the-original-variable
And this was me (8 - in the comments):Quote8
I just spend the past 3 hours trying to debug my program... I thought I was going mad. This was the most infuriating bug I have EVER ran into. Wow... – 43.52.4D. Mar 17 '16 at 7:05
12
@43.52.4D. Only it's not a bug :-) – yuvi Mar 17 '16 at 7:13
Not really me but this is where my mind was.
:)
What aboutThe assignment with the optional call to UniqueString is the recommended solution. The call to Copy is a bit more complex as the mere assignment even if both allocate a full, new string.Code: [Select]B := copy(A, 1, Length(A));
?
OK - good luck with your dead language. I'm outta here.It's not quite dead, at least not yet and, something about the language must have brought you here.