Buffered Files & Associative Arrays

Michael mcoupland at gmail.com
Tue Jan 22 22:37:29 PST 2008


Wow, yeah I think that's pretty unfortunate. I haven't done much D coding, and was only tangentially aware of the copy-on-write nature of D arrays (which I think is the underlying cause of this bug/feature...?)

This seems to seriously violate the principle of least surprise: I strongly suspect that most non-D programmers would make the same assumption I did. It's one thing when you're passing around a bunch of char*'s; but this is a full featured string class!

Chalk it up to the pains of learning D if you want, but I'm not confident I won't make this mistake numerous times (resulting in potentially strange and hard-to-solve bugs) before getting it straight in my head, which is very frustrating... :(

bearophile Wrote:

> Unknown W. Brackets:
> > As it happens, the problem is the way you are abusing File's buffer. 
> > You're taking the line, and using it... where the stream is overwriting 
> > that space with new data.
> 
> Yes, D is rather unsafe in that regard. To avoid this kind of bugs I add a "bool copy=true" as a template parameter (constant at compile time) to all my classes that return iterable objects then manage lot of data. So by default they perform the copy, and you avoid that whole class of bugs. When you know what you are doing and you want to go faster (sometimes 10 times faster) accepting a bit less safe code, you set that copy flag to false, and it keeps using the same buffer. I think the Phobos can grow such extra parameter in its iterable objects to avoid such kind of bugs.
> 
> Bye,
> bearophile




More information about the Digitalmars-d mailing list