ostream&

This is a discussion on ostream& within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this following method, wanting to add a string to a std stream the function toString() returns a ...

  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
    21,632
    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*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21