Clear a stringstream

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

This is a subtle variation of a question I posted some time ago.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();
s.str().erase();
std::cout << "s=" << s.str();
s.str( "" );
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following output:

s=
s=Hello, world!
s=Hello, world!
Exception catch: unexpected NULL pointer in function: basic_string( const charT* ,size_type,const Allocator&)

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?
 
V

Victor Bazarov

Christopher said:
This is a subtle variation of a question I posted some time ago.

#include <map>

I don't think anything from that header is used in your program.
#include <iostream>
#include <sstream>

Add

#include <string>
#include <exception>

(just in case)
int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();
s.str().erase();

Why are you calling 'erase()' here? Haven't you been told to do

s.str(std::string());

?
std::cout << "s=" << s.str();
s.str( "" );

That should work fine too.
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following output:

s=
s=Hello, world!
s=Hello, world!
Exception catch: unexpected NULL pointer in function: basic_string( const charT* ,size_type,const Allocator&)

Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?

I compiled your code (with my corrections) with VC++ v6 (not the best
compiler/library combo out there), and it ran fine (and no exceptions
occurred).

What _is_ that implementation? Please tell us so we could avoid it
like the plague. And get yourself a decent one, before it's too late.

Victor
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?

Hahahahahahahaha, so much for the stringstream * idea.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::eek:stringstream *os=new std::eek:stringstream();
std::cout << "Hello, world!\n";
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

This seemingly trivial program crashes (doesn't even throw the
exception, and makes Windows try to send an error report) when linked
with the company library that every program must use.

I'm going to get my resume updated this weekend.
 
R

Rob Williscroft

Christopher Benson-Manica wrote in in
comp.lang.c++:
This is a subtle variation of a question I posted some time ago.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::stringstream s;
std::cout << "s=" << s.str() << "\n";
s << "Hello, world!\n";
std::cout << "s=" << s.str();

This erases a *copy* of the sequence in 's':
s.str().erase();

To reset the sequence use:

s.str( "" );


std::cout << "s=" << s.str();
s.str( "" );
std::cout << "s=" << s.str() << std::endl;
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

When compiled with my broken implementation, I get the following
output:

s=
s=Hello, world!
s=Hello, world!

All as expect so far.
Exception catch: unexpected NULL pointer in function: basic_string(
const charT* ,size_type,const Allocator&)

Probably as you erase()'d the temporary returned by str(), but it
shouldn't happen.
Is there any way to coerce my God-forsaken implementation to actually
clear the stringstream? Or will I have to use a stringstream* and
create a new one when I want a clean one?

if str( "" ) doesn't work try str( " " ) (note the space) I've found
1 implementation where this was required.


Rob.
 
V

Victor Bazarov

Christopher said:
Hahahahahahahaha, so much for the stringstream * idea.

#include <map>
#include <iostream>
#include <sstream>

int main(void)
{
try {
std::eek:stringstream *os=new std::eek:stringstream();
std::cout << "Hello, world!\n";
return 0;
}
catch( const std::exception &e ) {
std::cerr << "Exception catch: " << e.what() << std::endl;
}
return 0;
}

This seemingly trivial program crashes (doesn't even throw the
exception, and makes Windows try to send an error report) when linked
with the company library that every program must use.

I know that if compiled by itself by VC++ v6 and then executed,
it doesn't do that and simply displays "Hello, world!"

Could it be that the "company library" has a faulty memory manager
in it that chokes on 'new std::eek:stringstream()' somehow?...
I'm going to get my resume updated this weekend.

Never a bad idea to keep it up to date. Speaking from experience
here :-(

V
 
C

Christopher Benson-Manica

Victor Bazarov said:
I don't think anything from that header is used in your program.

My mistake.
Why are you calling 'erase()' here? Haven't you been told to do
s.str(std::string());

It doesn't work :(
That should work fine too.

I know it...
What _is_ that implementation? Please tell us so we could avoid it
like the plague. And get yourself a decent one, before it's too late.

Borland C++ compiler 5.4. 5.5.1 compiles the code correctly, but
compiling all our 5.4 code is a major hassle that I haven't resolved
yet. I haven't gotten a good answer for why we're two versions behind
the current C++ Builder, besides the fact that no one wants to make
the transition.
 
C

Christopher Benson-Manica

Rob Williscroft said:
To reset the sequence use:
s.str( "" );

Except it doesn't work here...
if str( "" ) doesn't work try str( " " ) (note the space) I've found
1 implementation where this was required.

That was the hack I vaguely remembered but couldn't find in the Google
archives. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top