D array expansion and non-deterministic re-allocation
Steven Schveighoffer
schveiguy at yahoo.com
Wed Nov 25 21:48:03 PST 2009
Bartosz Milewski Wrote:
> The problem here is that the guy is sort of right. Indeed quote() expands the slice before overwriting it and that requires re-allocation to avoid stomping over the original buffer. There is however a boundary case, when the whole buffer matches the pattern. Then the expansion is done in place and the buffer is modified. This would never happen if expansion were guaranteed to re-allocate.
Actually, the requirement for not reallocating is that the slice ends at the end of an allocated block, so for example if the pattern matched at the end of the buffer, you may have problems.
The larger problem in the code is that the developer expects reallocation when it is not guaranteed. That is, he is *depending* on reallocation. In that case, you should explicitly reallocate. This doesn't necessarily mean using .dup, the following works just fine:
return '"' ~ word ~ '"';
And it actually performs much better than the example. In all honesty, I was sort of trying to play your game for this example. In real life, I would have mentioned that the above line replaces the quote function, and most likely the coder would have accepted it not because it deterministically allocates but because it's cleaner and shorter :)
>
> What this example was supposed to illustrate is that it's easy to forget to explicitly duplicate an array, especially if it's not clear who's responsible--the caller or the callee.
I don't think the example illustrates that. It illustrates that there are convoluted ways to try and explicitly use appending to duplicate arrays, and why one shouldn't do that.
-Steve
More information about the Digitalmars-d
mailing list