Why was std::strstream deprecated?

C++StringstreamStrstream

C++ Problem Overview


I recently discovered that std::strstream has been deprecated in favor of std::stringstream. It's been a while since I've used it, but it did what I needed to do at the time, so was surprised to hear of its deprecation.

My question is why was this decision made, and what benefits does std::stringstream provide that are absent from std::strstream?

C++ Solutions


Solution 1 - C++

The strstream returned a char * that was very difficult to manage, as nowhere was it stated how it had been allocated. It was thus impossible to know if you should delete it or call free() on it or do something else entirely. About the only really satisfactory way to deallocate it was to hand it back to the strstream via the freeze() function. This was sufficiently non-obvious, that lots of people got it wrong. The stringstream returns a string object which manages itself, which is far less error prone.

There was also the issue of having to use ends to terminate the string, but I believe the deallocation problem was the main reason for deprecation.

Solution 2 - C++

Easier to understand memory management. (Can someone remember who is responsible for freeing the allocated memory and in which conditions?)

(Note that as strstream still provide something which is not available elsewhere, it will continue to be present in C++0X -- at least last time I checked the draft it was).

Solution 3 - C++

A strstream builds a char *. A std::stringstream builds a std::string. I suppose strstreams are deprecated becuase of the potential for a buffer overflow, something that std::string automatically prevents.

Solution 4 - C++

From a personal perspective on more than one occasion I've seen obscure memory corruptions that took days or weeks to track down and eventually came down to use of strstream. As soon as it was replaced with stringstream the corruptions vanished and I didn't ask any more questions! That was enough for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandandView Question on Stackoverflow
Solution 1 - C++anonView Answer on Stackoverflow
Solution 2 - C++AProgrammerView Answer on Stackoverflow
Solution 3 - C++Ken BloomView Answer on Stackoverflow
Solution 4 - C++Component 10View Answer on Stackoverflow