Re: ostringstream output to C-string
Fab wrote:
I'm using std::ostringstream in my program to format a string. In my
unit tests I use it quite extensively to generate expected results
that I then compare with a predefined string.
The problem is that this code does something wrong:
std::ostringstream os;
os << "Hello world";
const char * const produced = os.str().c_str();
if (strcmp(produced, "Hello world") == 0) {...}
In particular, the 'produced' buffer is invalid.
On Linux and Mac OS X the program run and any access to 'produced'
buffer, does not report any error, but if you run a test program with
valgrind, Guard Malloc or any other memory checker tools, all the
tools report some problems.
The workaround to this problem is to generate a temporary variable to
store a reference to the the std::string produced by os.str(). In this
case everything works as expected:
std::ostringstream os;
os << "Hello world";
const std::string & tempString(os.str());
const char * const produced = os.str().c_str();
if (strcmp(produced, "Hello world") == 0) {...}
This is nothing different from the other code, from the language
point of view. The expression that used to initialise 'produced'
can still create another temporary, which is going to be destroyed
at the end of initialising 'produced', thus making 'procuded'
an invalid pointer. You just get lucky here and not the first
time around.
What you want to do, perhaps is
const char* const procuded = tempString.c_str();
Then you're using the actual temporary object that 'tempString'
is bound to, and which will survive as long as 'tempString' does.
Is it because os.str().c_str() generates a temporary std::string to
store the produced string, and c_str() returns a pointer to this temp
object?
Yes, pretty much.
Shouldn't temp object get disposed when exiting the scope?
Not sure how to answer this question (about the scope) because I
don't understand what you're asking. The temp object created in
the expression used to evaluate 'produced' survives only until
the 'produced' gets fully initialised.
Does it mean that this temp string's scope is only the 'produced'
assignment and after that is no longer valid?
It's not assignment, it's initialisation. And, yes, it's destroyed
right after 'produced' is initialised.
What's your opinion on that?
No opinion. It's all according to the written rules.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask