Why does stringstream give strange values?

So, I'm trying to use stringstream in my program to get some formatted string (for example, because I need to convert numbers to string).

The fact is that my program worked, and when I debug it using Visual Studio, in my Spy window I see that my stringstream buffer buffer gives (when I try to add "Framerate" to it):

"FramerateÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý" "" "" "îþîþ"

Code:

std::stringstream s; s << "Framerate"; 

My code will then work when I try to get the associated string ... I cannot find any explanation for this, nor a problem with my code ...

EDIT: It turns out these characters are not a problem and that the string stream returns the expected string using str() .

+4
source share
2 answers

stringstream internal buffer does not have to be null-terminated. However, when you call str() on a stringstream object, you will get a null-terminated string. Probably the cause of your accident elsewhere.

+5
source

In Visual Studio 2015 version 14.0.25431.01 Update 3, it looks like std :: strstream does not guarantee that the data returned by 'str ()' ends with zero.

I checked the test with

 `std::strstream s; s << "the cow jumped over the moon!"; return s.str();` 

and got: the cow jumped over the moon!===zzzz

by adding s.write("", 1); before returning, fixing my conclusion, but very unpleasant.

0
source

All Articles