Ostrstream unpredicted behavior in VS 2008

Discussion in 'C++' started by Saurabh Gupta, Feb 15, 2011.

  1. 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?
    Saurabh Gupta, Feb 15, 2011
    #1
    1. Advertising

  2. 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*).
    Robert Hairgrove, Feb 15, 2011
    #2
    1. Advertising

  3. 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!
    Robert Hairgrove, Feb 15, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Prince

    How to reuse a ostrstream?

    Charles Prince, May 14, 2004, in forum: C++
    Replies:
    9
    Views:
    4,929
    Siemel Naran
    May 17, 2004
  2. becte

    ostrstream question

    becte, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    1,001
    Victor Bazarov
    Oct 29, 2004
  3. Replies:
    2
    Views:
    4,404
    Dietmar Kuehl
    Jan 11, 2005
  4. Mathieu Malaterre

    ostringstream / ostrstream problem

    Mathieu Malaterre, Mar 3, 2005, in forum: C++
    Replies:
    3
    Views:
    5,127
    Dietmar Kuehl
    Mar 4, 2005
  5. Pep
    Replies:
    2
    Views:
    397
Loading...

Share This Page