Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Ostrstream unpredicted behavior in VS 2008

Reply
Thread Tools

Ostrstream unpredicted behavior in VS 2008

 
 
Saurabh Gupta
Guest
Posts: n/a
 
      02-15-2011
ostrstream m_msgStream;
m_msgStream.seekp(0);
m_msgStream << "Hello";
m_msgStream << ends;
char *str = m_msgStream .str();

We are getting str NULL. If we remove the skeep line then it working
fine. Even the same code is working fine with VS 6. Anyidea how to use
seekp in VS 2008?
 
Reply With Quote
 
 
 
 
Robert Hairgrove
Guest
Posts: n/a
 
      02-15-2011
On 02/15/2011 11:44 AM, Saurabh Gupta wrote:
> ostrstream m_msgStream;
> m_msgStream.seekp(0);
> m_msgStream<< "Hello";
> m_msgStream<< ends;
> char *str = m_msgStream .str();
>
> We are getting str NULL. If we remove the skeep line then it working
> fine. Even the same code is working fine with VS 6. Anyidea how to use
> seekp in VS 2008?


Why use it at all? At least it is entirely superfluous in the example
code you posted.

Aside from that, you should realize that std::stringstream::str()
returns std::string, not char*. You should be using:

const char *str = m_msgStream.str().c_str();

Please note that modifying the contents of the return value of
std::string::c_str(), as might happen after the above assignment to
non-const char* (which is allowed for legacy reasons on many compilers),
will result in undefined behavior (c_str() returns const char*).
 
Reply With Quote
 
 
 
 
Robert Hairgrove
Guest
Posts: n/a
 
      02-15-2011
On 02/15/2011 07:13 PM, Paavo Helde wrote:
> Robert Hairgrove<> wrote in news:14522$4d5ab628
> $d9a2e747$:
>
>> On 02/15/2011 11:44 AM, Saurabh Gupta wrote:
>>> ostrstream m_msgStream;

>
> ostrstream has been deprecated for a long time already, I would suggest
> to switch over to ostringstream.
>


Oops ... overlooked that one!

>> Aside from that, you should realize that std::stringstream::str()
>> returns std::string, not char*. You should be using:

>
> He is using ostrstream, where indeed str() returns char*.


Oops again.

>> const char *str = m_msgStream.str().c_str();

>
> With ostringstream this would create a dangling pointer into a destroyed
> temporary std::string object!


And yet another oops!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2008 new year,2008 new business, 2008 new life, much cheap andbeautiful product will help you yhnetstore@gmail.com Digital Photography 0 01-07-2008 04:57 PM
strstream and ostrstream with new g++ b83503104@yahoo.com C++ 2 01-11-2005 09:51 AM
ostrstream question becte C++ 1 10-29-2004 03:48 PM
How to reuse a ostrstream? Charles Prince C++ 9 05-17-2004 05:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57