Strange char[] behaviour, Re: Documentation bugs

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Fri Mar 10 03:07:03 PST 2006


Thomas Kuehne wrote:
> Jari-Matti Mýkelý schrieb am 2006-03-10:
>>> char[] teststring = "testing";
>>> doAlgorithm(teststring);
>>> writefln("%s", teststring); // segfault here.
>>>
>>> the algorithm doesn't write to the string, change its length nor does it
>>> free/delete it in any way. Just plain slicing and reference passing.
>>> Very weird.
> 
> Please replace
>>> doAlgorithm(teststring);
> with 
>>> doAlgorithm(teststring.dup);
> 
> I know the behaviour shouldn't change as "the algorithm doesn't write to
> the string", but ...

I tried that one too. Didn't help in any way. The only
assignment-operations I have are the following:

  class Node {
    char[] label;
    this(char[] foo) { label = foo.dup; }
    void shorten(int c) { label = label[c..$].dup; }
  }

Somehow I though that the CoW prevents the misuse of strings. The
dchar-version works perfectly without .dup-cloning on any arbitrary long
input. The char-version doesn't work, if too many iterations/nodes are
created, but works perfectly when run once with short input strings. The
maximum input string length is then a constant. I guess it might be
valuable to end up with a short test case here. I'll try my best to
create one.

-- 
Jari-Matti



More information about the Digitalmars-d-bugs mailing list