Memory deallocation issues with ostringstream

Discussion in 'C++' started by Generic Usenet Account, Jan 24, 2007.

  1. With the deprecated ostrstream class, when the constructor is invoked
    without arguments, memory is dynamically allocated. In that case the
    onus on freeing the memory lies with the user. Typically this is done
    by obtaining the char buffer (by invoking the str() method) and then
    explicitly deleting it.

    Does the ostringstream class also have the same issue? I mean, if I
    instantiate ostringstream without any constructor arguments, is the
    onus of freeing the dynamically allocated memory still on me?

    If my question is not clear, I hope the following code snippet will
    help. I need the answer to the question
    // ******* Do we need to do something similar for
    ostringstream? *******

    in the sample code.

    Thanks,
    Song

    ///////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <ctime>

    #ifdef DEPRECATED
    #include <strstream>
    #else
    #include <sstream>
    #endif // DEPRECATED

    using namespace std;

    main()
    {
    #ifdef DEPRECATED
    ostrstream os;
    #else
    ostringstream os;
    #endif // DEPRECATED

    time_t now;
    time(&now);
    os << "Hello World to all of you on " << ctime(&now);
    cout << os.str();

    #ifdef DEPRECATED
    char* sbuf = os.str();
    delete [] sbuf; // Deallocating dynamically
    allocated memory
    #else
    // ******* Do we need to do something similar for ostringstream?
    *******
    #endif // DEPRECATED
    }
     
    Generic Usenet Account, Jan 24, 2007
    #1
    1. Advertising

  2. Generic Usenet Account

    Rolf Magnus Guest

    Generic Usenet Account wrote:

    > With the deprecated ostrstream class, when the constructor is invoked
    > without arguments, memory is dynamically allocated. In that case the
    > onus on freeing the memory lies with the user. Typically this is done
    > by obtaining the char buffer (by invoking the str() method) and then
    > explicitly deleting it.
    >
    > Does the ostringstream class also have the same issue?


    No. It handles its memory automatically.

    > I mean, if I instantiate ostringstream without any constructor arguments,
    > is the onus of freeing the dynamically allocated memory still on me?


    No.

    > main()


    Btw: Functions without a return type are not just deprecated, but not
    allowed at all in C++.
     
    Rolf Magnus, Jan 24, 2007
    #2
    1. Advertising

  3. Generic Usenet Account

    Guest

    On Jan 24, 12:29 pm, Rolf Magnus <> wrote:
    > > Does the ostringstream class also have the same issue?

    > No. It handles its memory automatically.


    I have seen several snippets of code where programmers have dome
    something like this:
    ostringstream strstrm;
    strstrm << ......;
    strstrm.str(""); // <<----------<<


    So, is the third statement really uncalled for?

    Thanks,
    Song
     
    , Jan 24, 2007
    #3
  4. Generic Usenet Account

    Marcus Kwok Guest

    In comp.lang.c++ wrote:
    > On Jan 24, 12:29 pm, Rolf Magnus <> wrote:
    >> > Does the ostringstream class also have the same issue?

    >> No. It handles its memory automatically.

    >
    > I have seen several snippets of code where programmers have dome
    > something like this:
    > ostringstream strstrm;
    > strstrm << ......;
    > strstrm.str(""); // <<----------<<
    >
    >
    > So, is the third statement really uncalled for?


    Well, it depends on what it's being used for. Sometimes if the
    stringstream is being reused, then strstrm.str(""); followed by
    strstrm.clear(); will allow you to reuse it (instead of constructing a
    new one, for example in the body of a loop). But it is not necessary to
    use it to reclaim memory, since the stringstream will take care of it.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Jan 24, 2007
    #4
    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. qazmlp
    Replies:
    1
    Views:
    748
    John Harrison
    Mar 7, 2004
  2. Replies:
    2
    Views:
    728
    Paavo Helde
    Dec 18, 2004
  3. BigBrian
    Replies:
    12
    Views:
    590
    Pete Becker
    Apr 7, 2005
  4. Nicolas Matringe

    std.textio, readline and memory deallocation

    Nicolas Matringe, Sep 1, 2006, in forum: VHDL
    Replies:
    9
    Views:
    2,105
    Paul Uiterlinden
    Sep 4, 2006
  5. pasa_1
    Replies:
    19
    Views:
    519
    David Harmon
    Nov 2, 2006
Loading...

Share This Page