Thread: ostream&

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    ostream&

    Hi,
    I have this following method, wanting to add a string to a std stream
    the function toString() returns a pointer to a string. the question is, when I add it to a stream, does it create a copy or add the current pointer, and therefore, what is the correct time to release the allocated memory of this pointer?
    I just don't get this whole streaming thing...
    Code:
    //Edge.h
    class Edge
    {
    	char* toString() const;
    };
    #endif
    
    //EdgeNode.h
    class EdgeNode
    {
    	friend std::ostream& operator << (ostream& out,const EdgeNode  &eNode);
    };
    //EdgeNode.cpp
    #include EdgeNode.h
    ostream& operator << (ostream &out, const EdgeNode &eNode)
    {
    	char *p=eNode.m_edge.toString();
    	return out << p;
    	delete [] p;
    }
    //EdgeList.h
    class EdgeList
    {
             friend std::ostream& operator << (ostream& out,EdgeList eList &);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The answer to your direct question: When the call to operator<<(ostream &, char *) returns, the char * passed in is no longer needed to produce correct output (a copy is made of the content pointed to by char *, or the data has actually been sent to the output device already.

    Would it be difficult to make eNode.m_edge.toString return a std::string, instead of a char pointer? If so, you would avoid the problem of deallocating completely.

    Your current code is flawed in that it returns before it deletes p, so it will definitely leak memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    this is a school assignment, a requirement is that the is a method that returns a char*
    can i not do somthing like:
    Code:
    out = out << p;
    delete [] p;
    return out;

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't need the out = part.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    so if i have a linkedList

    can i do somthing like
    Code:
    ostream& operator << (ostream& out,const EdgeList & eList)
    {
    	EdgeNode *current=eList->getHead();
    
    	while (current!=NULL)
    	{
    		out << current << ',';
    		current=current->getNext();
    	}
             return out;
    }
    Last edited by msshapira; 05-07-2009 at 10:07 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That looks like it should be correct, except that getHead() and getNext() should be overloaded such that if the EdgeList object is const, they would return const EdgeNode* instead of EdgeNode*. Then you would change current to be a const EdgeNode*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ostream& outrage !!!
    By xephyr in forum C++ Programming
    Replies: 3
    Last Post: 09-20-2004, 04:17 PM