🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3435

Re: OSTRSTREAM and sizing

#3435
From: Steve Clamage
Date: Sat, 24 Feb 1996 17:15
82 lines
3664 bytes
In article sfr@venus.roc.csci.csc.com, tottinge@csci.csc.com (Tim Ottinger) writes:

>This might be a silly question, but a coworker of mine noticed this, and we've 
>tested it on two compilers with similar results:

>	#include <iostream.h>
>	#include <strstream.h>
>	int main (char * argc, char * argv[])
>	{
> 	  ostrstream str("abcdefg", 4);
>   
>  	 cout << "Length = " << str.pcount() << endl;
>  	 cout << "Data   = " << str.str();
>	}

>produces the following output on both the GNU and HP C++ compilers
>	Length = 0
>	Data   = abcdefg

>Two things were puzzling about this.  

>The first was that the length parameter on the c'tor didn't seem 
>to be used for anything, and the second was that I couldn't spot the 
>right way to tell how big the buffer in the strstreambuf is (how many 
>actual characters are in it).

In the example code, you create an ostrstream, and pass in a fixed
buffer. You tell the ostrstream that the buffer can hold 4 characters,
including a terminating null, if any. You don't write anything to
the stream. By a quirk of fate, the data area is actually 8 bytes
in length, and contains "abcdefg" plus a null, but the ostrstream
doesn't know that. (You can tell the ostrstream that the buffer begins
with a null-terminated string by setting the "ate" or "app" mode bits,
but in this case you would have a problem since the null comes after
the 4th character.)

When you call pcount(), it tells you how many characters have been
written to the stream, which is zero. When you call str(), it just
returns a pointer to the beginning of the buffer. The buffer contains
whatever it contains. In this case it contains "abcdefg" plus a null,
since you did nothing to change that coincidental fact.

If instead of "abcdefg" the buffer was an uninitialized auto variable,
you would also get a zero return from pcount(), but when you tried
to print the result of str() there is no telling what would happen.
Typically it would print a bunch of garbage, often followed by a
memory fault.

You might find this variation on your program more interesting:

#include <strstream.h>
int main ()
{
	char buf[20] = "abcdefg";
	ostrstream str(buf, 20, ios::out|ios::app);

	cout << "Length = " << str.pcount() << endl;
	cout << "Data   = " << str.str() << endl;

	str << "zyx" << ends; // appends to existing null-terminated data
	cout << "Length = " << str.pcount() << endl;
	cout << "Data   = " << str.str() << endl;
	return 0;
}

Result:
Length = 7         -- initially contains 7 characters, null is not part of string
Data   = abcdefg
Length = 11        -- we added 3 characters plus a null (which is part of the string)
Data   = abcdefgzyx

---
Steve Clamage, stephen.clamage@eng.sun.com



[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID: <4glkpb$c8q@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <4glcfq$sfr@venus.roc.csci.csc.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: clamage@Eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: OSTRSTREAM and sizing
Date: 24 Feb 1996 17:15:11 GMT
Organization: Sun Microsystems Inc.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <4glkpb$c8q@engnews1.Eng.Sun.COM>
References: <4glcfq$sfr@venus.roc.csci.csc.com>
Reply-To: clamage@Eng.sun.com
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 2711
X-Lines: 75
Originator: clamage@taumet