Why is std :: strstream not recommended?

I recently discovered that std::strstream deprecated in favor of std::stringstream . It has been some time since I used it, but it did what I needed to do at that time, so I was surprised to hear about it.

My question is: why was this decision made and what are the advantages of std::stringstream that are not available in std::strstream ?

+61
c ++ stringstream
May 12, '10 at 15:21
source share
4 answers

strstream returned a char * , which was very difficult to manage with, since it was not indicated anywhere how it was allocated. Thus, it was impossible to find out whether to remove it or call free () on it or do something else completely. The only really satisfactory way to free it was to return it back to strstream using the freeze() function. It was not obvious enough that many people were mistaken. stringstream returns a string object that controls itself, which is much less error prone.

There was also the problem of using ends to break the line, but I believe that the release problem was the main cause of obsolescence.

+86
May 12, '10 at 15:29
source share

Easier to understand memory management. (Can anyone remember who is responsible for freeing the allocated memory and under what conditions?)

(Note that since strstream still provides something that is not available elsewhere, it will still be present in C ++ 0X - at least the last time I checked the draft).

+15
May 12, '10 at 15:26
source share

A strstream builds a char * . A std::stringstream creates std::string . I believe strstream deprecated due to the possibility of a buffer overflow, which automatically prevents std::string .

+10
May 12 '10 at 3:25 a.m.
source share

From a personal point of view, I have repeatedly seen obscure memory corruptions that took days or weeks to track down and eventually went down to using strstream . As soon as it was replaced by stringstream , corruption disappeared, and I no longer asked any questions! That was enough for me.

+6
15 Oct '10 at 10:04
source share



All Articles