Buffered Files & Associative Arrays

bearophile bearophileHUGS at lycos.com
Tue Jan 22 01:29:40 PST 2008


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