🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by joost witteveen Mon, 27 Nov 1995 21:37
initialization of ostrstream
#836
Author: joost witteveen
Date: Mon, 27 Nov 1995 21:37
34 lines
1359 bytes
If I construct a ostrstream, with
  ostrstream os (s,sizeof(s))
do I have to clean ALL of s before I use it?
I'm getting strange results (i.e. I have to init ALL of s to 0)
with g++ 2.7.1, and 2.3.3.

So, my question is: is this a bug in g++, or am I missing something
about the string streams?


The following programme has this what I think is strange behaviour,
as it outputs in the last line not only the "hello, world", but
part of the old h's string s was inititalized to,  concatenated to it.

Thanks for any suggestions.


#include <iostream.h>
#include <strstream.h>
int main(int,char **){
  int i;
  char s[]="\000hhhhhhhhhhhhhhhhhhhhhhhhhhhh";
  cout<<"initial s:"<<s<<endl; //an empty string here.

  ostrstream os(s,sizeof(s));
  os<<"hello, world";
  cout<<s<<endl;          //outputs: hello, worldhhhhhhhhhhhhhhhhh
}

-- 

joost witteveen
            joost@rulcmc.leidenuniv.nl
          joostje@dds.hacktic.nl
Re: initialization of ostrstream
#837
Author: Kit Kauffmann
Date: Mon, 27 Nov 1995 15:38
49 lines
1859 bytes
In article <199511272135.WAA02816@rulcmc.LeidenUniv.nl> joost@rulcmc.leidenuniv.nl (joost witteveen) writes:
>From: joost@rulcmc.leidenuniv.nl (joost witteveen)
>Subject: initialization of ostrstream
>Date: 27 Nov 1995 16:37:47 -0500

>If I construct a ostrstream, with
>  ostrstream os (s,sizeof(s))
>do I have to clean ALL of s before I use it?
>I'm getting strange results (i.e. I have to init ALL of s to 0)
>with g++ 2.7.1, and 2.3.3.

>So, my question is: is this a bug in g++, or am I missing something
>about the string streams?


>The following programme has this what I think is strange behaviour,
>as it outputs in the last line not only the "hello, world", but
>part of the old h's string s was inititalized to,  concatenated to it.

>Thanks for any suggestions.


>#include <iostream.h>
>#include <strstream.h>
>int main(int,char **){
>  int i;
>  char s[]="\000hhhhhhhhhhhhhhhhhhhhhhhhhhhh";
>  cout<<"initial s:"<<s<<endl; //an empty string here.

>  ostrstream os(s,sizeof(s));
>  os<<"hello, world";
>  cout<<s<<endl;          //outputs: hello, worldhhhhhhhhhhhhhhhhh
>}

>-- 

>joost witteveen
>            joost@rulcmc.leidenuniv.nl
>          joostje@dds.hacktic.nl


You are missing an ends (insertor for the string terminator)



Kit Kauffmann - kitk@mudshark.sunquest.com
AKA 73363,447 (Compu$erve)

Finger me for my public key
Re: initialization of ostrstream
#839
Author: Enno Sandner
Date: Tue, 28 Nov 1995 08:12
37 lines
1747 bytes
In article <199511272135.WAA02816@rulcmc.LeidenUniv.nl> joost@rulcmc.leidenuniv.nl (joost witteveen) writes:

   If I construct a ostrstream, with
     ostrstream os (s,sizeof(s))
   do I have to clean ALL of s before I use it?
   I'm getting strange results (i.e. I have to init ALL of s to 0)
   with g++ 2.7.1, and 2.3.3.

   So, my question is: is this a bug in g++, or am I missing something
   about the string streams?


   The following programme has this what I think is strange behaviour,
   as it outputs in the last line not only the "hello, world", but
   part of the old h's string s was inititalized to,  concatenated to it.

   Thanks for any suggestions.


   #include <iostream.h>
   #include <strstream.h>
   int main(int,char **){
     int i;
     char s[]="\000hhhhhhhhhhhhhhhhhhhhhhhhhhhh";
     cout<<"initial s:"<<s<<endl; //an empty string here.

     ostrstream os(s,sizeof(s));
     os<<"hello, world";
     cout<<s<<endl;          //outputs: hello, worldhhhhhhhhhhhhhhhhh
   }

You've missed to terminate the string. Use 'ends' for an additional
0-char at the end of the string, ie. exchange 'os<<"hello, world";'
by 'os<<"hello, world"<<ends;'.

	regards
		Enno
Re: initialization of ostrstream
#842
Author: Bernd Eggink
Date: Tue, 28 Nov 1995 13:18
46 lines
1931 bytes
joost witteveen (joost@rulcmc.leidenuniv.nl) wrote:
>  If I construct a ostrstream, with
>    ostrstream os (s,sizeof(s))
>  do I have to clean ALL of s before I use it?
>  I'm getting strange results (i.e. I have to init ALL of s to 0)
>  with g++ 2.7.1, and 2.3.3.

>  So, my question is: is this a bug in g++, or am I missing something
>  about the string streams?


>  The following programme has this what I think is strange behaviour,
>  as it outputs in the last line not only the "hello, world", but
>  part of the old h's string s was inititalized to,  concatenated to it.

>  Thanks for any suggestions.


>  #include <iostream.h>
>  #include <strstream.h>
>  int main(int,char **){
>    int i;
>    char s[]="\000hhhhhhhhhhhhhhhhhhhhhhhhhhhh";
>    cout<<"initial s:"<<s<<endl; //an empty string here.

>    ostrstream os(s,sizeof(s));
>    os<<"hello, world";
>    cout<<s<<endl;          //outputs: hello, worldhhhhhhhhhhhhhhhhh
>  }

You forgot to append a '\0', which is _not_ done automatically by
'ostrstream'. There is a special manipulator 'ends' which does exactly
this:

   os << "hello world" << ends;

Now the string is correctly '\0'-terminated.

	Bernd

--
+----------------------------------+
|          Bernd Eggink            |
|    Rechenzentrum Uni Hamburg     |
| admin@rzaix13.rrz.uni-hamburg.de |
+----------------------------------+
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