return my_ostringstream.str().c_str();

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jabbah
    New Member
    • Nov 2007
    • 63

    return my_ostringstream.str().c_str();

    I want to use a stringstream internally, but to conform to an api I want to return a char*.

    Only this does not work:

    [CODE=cpp]
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;

    class A
    {
    ostringstream my_ostringstrea m;
    public:
    void generate()
    {
    my_ostringstrea m << "do something";
    }
    const char* get()
    {
    return my_ostringstrea m.str().c_str() ;
    }
    };

    void main()
    {
    A a;
    a.generate();
    cout << "data: " << a.get() << "\n";
    }
    [/CODE]


    using ms visual studio 2005 I get a "debug assertion failed! [...] _BLOCK_TYPE_IS_ VALID(pHead->nBlockUse)" at runtime.

    Ok, so maybe my_ostringstrea m.str() creates a local copy and then returns a pointer to a local object? Is that the case? And anyways, can somebody give me a hint on how to resolve that?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Temporary objects go out of scope at the end of a statement so the pointers to them become garbage.

    Just don't rely on a compiler temporary.

    I changed your code to allow for a member variable.

    [code=cpp]
    class A
    {
    stringstream my_ostringstrea m;
    string temp;
    public:
    void generate()
    {
    my_ostringstrea m << "do something";
    }
    const char* get()
    {
    temp = my_ostringstrea m.str();
    return temp.c_str();
    }
    };
    [/code]

    Comment

    • jabbah
      New Member
      • Nov 2007
      • 63

      #3
      Originally posted by weaknessforcats
      I changed your code to allow for a member variable.
      Ok, that works. thanks for that.


      A related question: Is there a similar way if we are given a simple function instead of a class? for example:

      [code=cpp]
      const char* getadata()
      {
      ostringstream my_ostringstrea m;
      my_ostringstrea m << "do something";
      return my_ostringstrea m.str().c_str() ;
      }
      [/code]
      Ok, I do understand that this implementation cant possibly work, since it suffers from the same deficiency as my initial post. However, what I want is to return a char* and not force the caller to delete the returned value.
      Maybe, I guess, ... thats just not possible, right?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by jabbah
        Ok, I do understand that this implementation cant possibly work, since it suffers from the same deficiency as my initial post. However, what I want is to return a char* and not force the caller to delete the returned value.
        Maybe, I guess, ... thats just not possible, right?
        You would need to allocate memory on the heap for the char* string and copy the std::string to that memory. There is a string::copy() for that purpose.

        Then return the allocated address. Later someone else can delete it.

        Comment

        • jabbah
          New Member
          • Nov 2007
          • 63

          #5
          Originally posted by weaknessforcats
          Later someone else can delete it.
          That is, someone else *has* to delete it, right.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Yes, they do.

            Unless...

            You use a smart pointer that knows when to delete itself.

            There is an article (and template code) for a handle in the C/C++ HowTos forum article on Handle classes.

            Comment

            • jabbah
              New Member
              • Nov 2007
              • 63

              #7
              thanks weaknessforcats

              Comment

              Working...