in reply to Growing strings, avoiding copying and fragmentation?
As the data is compressed, you don't know the final size, so you can't preallocate the entire buffer. However, you probably do know the compressed size, and it is unlikely to shrink, so why not preallocate the buffer to the compressed size and then expand from there?
You might even add a scaling factor to the compressed size. If the algoritm used averages 50% compression on the type of data your handling, the double the compressed size and allocate. If this turns out to be an over estimate and your end up shrinking, the cost is likely to be much less than rallocing the buffer many times.
|
|---|