[Issue 2093] string concatenation modifies original

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 22 06:43:21 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=2093





------- Comment #7 from smjg at iname.com  2008-11-22 08:43 -------
(In reply to comment #4)
> Currently an 'owner' is anyone who has a pointer to array's beginning:
> 
> char[] s = "hello".dup;
> char[] s1 = s[0..4];
> s1 ~= "!";
> assert(s != s1); // fails, both are "hell!", s is overwritten

A simple char[] is fully mutable, so that doesn't violate any established rule,
but whether it's desirable is another matter.

With const(char)[] or invariant(char)[], obviously this isn't going to work, so
~= should always reallocate (unless the optimiser can be sure that no other
reference to the data can possibly exist).

Alternatively, the GC could maintain a note of the actual length of every
heap-allocated array.  Ownership would be determined by matching in both start
pointer and length.  When the length is increased, whether by .length or ~=,
either update this actual length (if it's the owner that we're extending, IWC
all other references to the same data lose ownership) or reallocate the array.


-- 



More information about the Digitalmars-d-bugs mailing list