If I pass an istreambuf_iterator
wrapper around a stringstream
to a function which requires such an iterator, how can I re-use (overwrite) the stream's existing buffer after the function has returned, without triggering a heap allocation?
(clear
seems to cause re-allocation when a new character is inserted.)
Asked
Active
Viewed 279 times
1

user541686
- 205,094
- 128
- 528
- 886
-
Unfortunately, I doubt there's much you can do. At least to my recollection, the standard doesn't specify much (anything?) in the way of requirements about when a streambuf can/can't reallocate. – Jerry Coffin Dec 17 '12 at 02:27
-
@JerryCoffin: I'm dealing with Visual C++ though, that's a lot more specific than generic C++. – user541686 Dec 17 '12 at 02:30
-
Thinking a bit more, have you tried just a `seekp(0)` to start writing from the beginning again? – Jerry Coffin Dec 17 '12 at 02:32
-
Have you tried managing the buffer explicitly with `rdbuf`? – Sergey Kalinichenko Dec 17 '12 at 02:36
-
@JerryCoffin: Huh... that seems to be doing the trick (I combined it with `seekg`). Although I'm still not sure since the CRT code is really hard to read and I haven't tested it a lot yet... but I'm not noticing any heap allocations at the moment. Thanks for the pointer (no pun intended). – user541686 Dec 17 '12 at 02:39
-
@dasblinkenlight: I had, but I wasn't sure what to do with it exactly. – user541686 Dec 17 '12 at 02:40