ostrstream and memory leak

Discussion in 'C++' started by tcl, Oct 4, 2005.

  1. tcl

    tcl Guest

    Questions on ostrstream.


    #1) do I have a memory leak as the control exits the scope
    {
    ostrstream os;
    os << "hello world" << endl << ends;
    }


    #2) if there's no memory leak in #1, is there a leak here:
    {
    ostrstream os;
    os << "hello world" << endl << ends;
    cout << os.str();
    }


    #3) if there's a memory leak in either #1 or #2, would adding
    either of the following statements right before exiting
    the scope fix it? if not, how to fix it?
    os.freeze(0);
    delete [] os;

    Thanks,
    tcl
    tcl, Oct 4, 2005
    #1
    1. Advertising

  2. tcl wrote:
    > Questions on ostrstream.
    >
    >
    > #1) do I have a memory leak as the control exits the scope
    > {
    > ostrstream os;
    > os << "hello world" << endl << ends;
    > }


    No.
    >
    >
    > #2) if there's no memory leak in #1, is there a leak here:
    > {
    > ostrstream os;
    > os << "hello world" << endl << ends;
    > cout << os.str();
    > }


    Unfortunately yes because ostrstream is very badly designed.

    >
    >
    > #3) if there's a memory leak in either #1 or #2, would adding
    > either of the following statements right before exiting
    > the scope fix it? if not, how to fix it?
    > os.freeze(0);


    I think the above is OK but consult your documentation.

    > delete [] os;


    The above is an error, probably shouldn't even compile.

    Better still use ostringstream, then you don't need to worry about any
    of this nonsense.

    john
    John Harrison, Oct 4, 2005
    #2
    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,928
    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,126
    Dietmar Kuehl
    Mar 4, 2005
  5. Pep
    Replies:
    2
    Views:
    397
Loading...

Share This Page