🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Tim Ottinger Fri, 23 Feb 1996 23:27
OSTRSTREAM and sizing
#3433
Author: Tim Ottinger
Date: Fri, 23 Feb 1996 23:27
52 lines
2611 bytes
Hi.

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).

It seems that "data" should have returned either "abcd", or "", 
rather than "abcdefg".  This tossed me.

My friend expected the 'pcount()' to tell him how many bytes are in
the buffer.

I can see some of this for istream(), though much still eludes.  For
an ostream, this is somewhat maddening.

Is is standard, and does it make sense?

******************************************************************************
* Tim Ottinger, Sr Tech				       tottinge@csci.csc.com *
* CSC Communications Industry Services		           217-351-8508x2420 *
* TRIS Division -- Cellular Billing and Support	            Fax 217-351-2640 *
******************************************************************************
* Requirements have always been the least stable part of any project I have  *
* worked upon.	                        -- Robert Martin via Usenet 12/02/95 *
******************************************************************************

[ 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
]
Re: OSTRSTREAM and sizing
#3435
Author: 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
]
Re: OSTRSTREAM and sizing
#3510
Author: James Kanze US/ESC 60/3/141 #407...
Date: Mon, 26 Feb 1996 19:49
62 lines
3493 bytes
In article <4glkpb$c8q@engnews1.Eng.Sun.COM> clamage@Eng.sun.com
(Steve Clamage) writes:

|> 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.

Just one small point which Steve forgot to mention (since it wasn't
really relevant to the question you asked): you'd better not try and
write anything to this stream, either, since it will result in
attempting to modify a string literal (undefined behavior according to
the standard, a segment violation in better compilers).

An interesting question: is ostrstream allowed to modify the parts of
the buffer which are presumed unwritten?  Could a legal implementation
of ostrstream, for example, start by zapping the buffer to '\0'?  If
this is the case, the above example invokes undefined behavior even
without writing to the stream.

(It's worth noting that in Steve's example of how to correctly use the
functions, the buffer is declared explicitly as an array, in order to
avoid the undefined behavior.)

--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
                -- A la recherche d'une activité dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads