TDPL bug or phobos bug?

flyinghearts flyinghearts at qq.com
Mon Nov 15 07:31:43 PST 2010


Page 8, in the example:

import std.stdio, std.string;

void main()
{
  uint[string] dictionary;
  foreach (line; stdin.byLine()) {
  // Break sentence into words
  // Add each word in the sentence to the vocabulary
    foreach (word; splitter(strip(line))) {
      if (word in dictionary) continue; // Nothing to do
      auto newlD = dictionary.length;
      dictionary[word] = newlD;
      writeln(newlD,word);
         //Notice: the following line is not exist in the book.
         //foreach(k, v; dictionary) writeln(k, " ", dictionary[k]);
    }
  }
}



There is a buffer reuse problem. The program does not work rightly on my
machine(win xp, DMD 2.050). Moreover, an error, "object.Error: Access
Violation", may occur when the associate array is directly accessed. It can be
tested just by uncommenting the line begin with "//foreach", and setting the
input data as:
a b
b c


The solution to the problem is:

dictionary[word]  ->   dictionary[word.idup]
or:
strip(line)       ->   strip(line.dup)





More information about the Digitalmars-d mailing list