[lug] simple iostream bug?

D. Stimits stimits at idcomm.com
Thu Mar 14 16:33:39 MST 2002


I'm trying to do something very very simple, and I know this should
work, but it doesn't. I'm thinking there is a bug in the iostream
library, maybe someone could tell me what I'm doing wrong here ("it
shouldn't happen"). Basically it is just a function to take a reference
to an ostream, insert some text, and return it by reference. In this
case it is cout. The code:

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

ostream& TextHeader( ostream& web_output )
{
   web_output << "Content-type: text/html\n" << endl;
   return web_output;
}

int main()
{
   cout << TextHeader( cout );
   cout << "<HTML><HEAD><TITLE>Testing</TITLE></HEAD>\n";
   cout << "<BODY><P>Testing</P></BODY></HTML>" << endl;

   return 0;
}
///////////////////////////////////////////////////////

What I expect to see:
Content-type: text/html

<HTML><HEAD><TITLE>Testing</TITLE></HEAD>
<BODY><P>Testing</P></BODY></HTML>

What I actually see:
Content-type: text/html

0xffffffff<HTML><HEAD><TITLE>Testing</TITLE></HEAD>
<BODY><P>Testing</P></BODY></HTML>


Notice the extra "0xffffffff" leading a line. I have tried variations
with flush(), and assertions on stream state being good. No luck, I
cannot get the 0xffffffff to disappear. What is interesting is that if I
modify the TextHeader function to simply pass along the reference to the
stream, without any modifications, insertions, operations...no changes
at all...the 0xffffffff still occurs. It appears that passing cout by
reference is sufficient to embed 0xffffffff, nothing else is required.
Does this seem to be a bug to anyone else? Or maybe there is something
more subtle going on?

One detail: In my setup, the TextHeader method is actually in a header
file all by itself, and I #include it at the top of main.cxx.

D. Stimits, stimits at idcomm.com



More information about the LUG mailing list