On Thu, Aug 26, 1999 at 10:03:21PM -0600, Robert Kerr wrote:
> Hi all,
> I'm having some problems converting my app to run under linux.  It runs
> alright under SGI, HP-UX, Solaris and WinNT, but it crashes beautifully
> under linux.
> 
> Anyway, I have a member variable called journalString of type ostrstream.
> I instantiate it so:
>     journalString = new ostrstream();
>     journalString->rdbuf()->setbuf(NULL, 50);
> 
> but when I try to use the << operator with it, it Segfaults.
> Example crashes:
> 
> *journalString << ends;
> *journalString << s << ' '; where s is a const char *
> *journalString << name;  where name is a const char *
> 
> Am I using ostrstream wrong?  Any ideas?

It looks to me as if you are doing too complicated things.  Is the
rdbuf->setbuf(NULL, 50) meant to allocated memory?  You don't need to do
that, the class is designed to take care of that by itself.

Is there a reason to have a member ostrstream pointer, instead of just
an ostrstream?  Typical use would be something like

ostrstream* journalString = new ostrstream();
string s = "something";
*journalString << "bla" << ' ' << s << ends;

journalString->c_str() returns the buffer as a C style string (char*).
If you use that, you need to deallocate the memory pointed to by c_str()
yourself, unless you later call

journalString->freeze(0);

Note that in the future the ostrstream will be replaced by
ostringstream.

Finally, segmentation faults can be due to memory corruption happening
elsewhere in the program, and only triggered in your ostrstream lines.
The fact that a program does not crash on other platforms does not mean
there are no bugs.  It is well possible that a memory corrupting bug
only bites you in linux.

HTH,
Eric

BTW, this is more of a question for a C++ newsgroup, like
comp.lang.c++.moderated.


-- 
 E.L. Meijer ([EMAIL PROTECTED])
 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)

Reply via email to