memory allocation question when using ostringstream in STL

Discussion in 'C++' started by sylcheung@gmail.com, Jan 10, 2006.

  1. Guest

    Hi,

    I am new to STL, and I have a memory allocation regarding using
    ostringstream in STL.

    I have a class called 'Rect' and it has a method like this:

    string Rect::toString() {
    ostringstream ost;
    ost << "x:" << x;
    ost << " y:" << y;
    ost << " w:" << w;
    ost << " h:" << h;

    return ost.str();
    }

    And in my program, I call toString() like this:
    Rect r;
    cout << r.toString();

    my question is who free up the memory used by the string return by
    ost.str()?

    Thank you.
    , Jan 10, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I am new to STL, and I have a memory allocation regarding using
    > ostringstream in STL.
    >
    > I have a class called 'Rect' and it has a method like this:
    >
    > string Rect::toString() {
    > ostringstream ost;
    > ost << "x:" << x;
    > ost << " y:" << y;
    > ost << " w:" << w;
    > ost << " h:" << h;
    >
    > return ost.str();
    > }
    >
    > And in my program, I call toString() like this:
    > Rect r;
    > cout << r.toString();
    >
    > my question is who free up the memory used by the string return by
    > ost.str()?
    >
    > Thank you.
    >


    The clean-up is performed by your C++ implementation, i.e. you do not
    have to do it yourself.

    Regards,
    Peter Jansson
    Peter Jansson, Jan 10, 2006
    #2
    1. Advertising

  3. Guest

    Thanks.

    But the variable "ost" (allocate on the stack) is out of scope as seen
    as the toString() is returned.
    then how can the value 'ost.str()' still be valid after the function
    toString() is returned?

    Thank you.
    , Jan 10, 2006
    #3
  4. Shezan Baig Guest

    wrote:
    > But the variable "ost" (allocate on the stack) is out of scope as seen
    > as the toString() is returned.
    > then how can the value 'ost.str()' still be valid after the function
    > toString() is returned?



    The toString() function returns by value. You can always return local
    stuff by value. If you return by reference, then it will not work.

    Hope this helps,
    -shez-
    Shezan Baig, Jan 10, 2006
    #4
  5. Stephen Howe Guest

    > But the variable "ost" (allocate on the stack) is out of scope as seen
    > as the toString() is returned.
    > then how can the value 'ost.str()' still be valid after the function
    > toString() is returned?


    It isn't. A temporary string is initialised with the copy of ost.str(), ost
    is destructed and it is the temporary string that is returned by toString().

    Stephen Howe
    Stephen Howe, Jan 10, 2006
    #5
  6. David Harmon Guest

    On 10 Jan 2006 11:21:27 -0800 in comp.lang.c++,
    wrote,
    >And in my program, I call toString() like this:
    >Rect r;
    >cout << r.toString();
    >
    >my question is who free up the memory used by the string return by
    >ost.str()?


    The return value from r.toString() is in the form of a temporary
    string object in the context of the full expression. It is a copy
    of the string from inside the function (although actual copying may
    be optimized away by very clever compiler.)
    At the end of the full expression the temporary is destroyed and its
    string class destructor frees any memory that it is holding.
    David Harmon, Jan 10, 2006
    #6
  7. Marcus Kwok Guest

    wrote:
    > I have a class called 'Rect' and it has a method like this:
    >
    > string Rect::toString() {
    > ostringstream ost;
    > ost << "x:" << x;
    > ost << " y:" << y;
    > ost << " w:" << w;
    > ost << " h:" << h;
    >
    > return ost.str();
    > }
    >
    > And in my program, I call toString() like this:
    > Rect r;
    > cout << r.toString();


    Others have answered your memory questions, but I thought I'd point out
    something, since you said you're new to STL.

    Usually, the "C++ way" of doing this is to define operator<< for your
    class, so that you can output the data to any ostream:


    #include <iostream>

    class Rect {
    public:
    friend std::eek:stream& operator<<(std::eek:stream& o, const Rect& r);
    Rect() : x(0), y(0), w(0), h(0) { }
    private:
    int x;
    int y;
    int w;
    int h;
    };

    std::eek:stream& operator<<(std::eek:stream& o, const Rect& r)
    {
    return o << "x:" << r.x
    << " y:" << r.y
    << " w:" << r.w
    << " h:" << r.h;
    }

    int main()
    {
    Rect r;
    std::cout << r << '\n';

    return 0;
    }

    --
    Marcus Kwok
    Marcus Kwok, Jan 10, 2006
    #7
    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. kwijibo28
    Replies:
    6
    Views:
    1,115
    tom_usenet
    Aug 11, 2003
  2. Riadh Elloumi

    Debug memory allocation with STL

    Riadh Elloumi, Jan 27, 2004, in forum: C++
    Replies:
    1
    Views:
    3,809
    tom_usenet
    Jan 27, 2004
  3. Ken
    Replies:
    24
    Views:
    3,828
    Ben Bacarisse
    Nov 30, 2006
  4. PhoneJack
    Replies:
    3
    Views:
    377
    Jonathan Mcdougall
    Jul 13, 2005
  5. chris
    Replies:
    6
    Views:
    969
    chris
    Oct 28, 2005
Loading...

Share This Page