Article View: gnu.g++.lib.bug
Article #306Re: ofstream bug in 2.1
From: Joe Buck
Date: Thu, 01 Oct 1992 00:37
Date: Thu, 01 Oct 1992 00:37
44 lines
1967 bytes
1967 bytes
In article <1aa7sjINN2rj@agate.berkeley.edu> faustus@ygdrasil.cs.berkeley.edu (Wayne A. Christopher) writes:
>The following program causes an assertion failure in the ofstream
>code. I can't see what is wrong... Any ideas? I don't have 2.2 yet
>so I haven't tested it there.
The crash also occurs with libg++ 2.2.
>#include <fstream.h>
>
>static ofstream ofile;
>
>main()
>{
> ofile = ofstream("outputfile");
> ofile << "Here is some output\n";
>}
Since there is no assignment operator for class ofstream, or for any of
its baseclasses, the assignment of ofstream("outputfile") does a
memberwise copy of fields to ofile. Since the right-side ojbect is a
temporary object, it is then destroyed, leaving ofile's members pointing
to garbage. It is then only a matter of time until the program blows up.
Assigning one ofstream to another is not valid.
The iostream library provided with cfront 2.1 handles this by making
ios::operator= a private member, which makes assignment of iostream
objects a compile-time error (except for classes like
{i,o,io}stream_withassign, which provide an assignment operator).
I think that libg++ should either do the same, or provide a correct
assignment operator. Same for the copy constructor.
libg++ does not provide the iostream_withassign classes. Should it?
I don't know.
You can probably get the effect you wanted by saying
ofile.open("outputfile");
--
Joe Buck jbuck@ohm.berkeley.edu
Technical Details
Message-ID:
<1adh8dINNmb8@agate.berkeley.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!convex!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!forney.berkeley.edu!jbuckReferences:
<1aa7sjINN2rj@agate.berkeley.edu>Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.lib.bug Path: arc1-ca.pugleaf.net!sparky!uunet!convex!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!forney.berkeley.edu!jbuck From: jbuck@forney.berkeley.edu (Joe Buck) Subject: Re: ofstream bug in 2.1 Message-ID: <1adh8dINNmb8@agate.berkeley.edu> Sender: gnulists@ai.mit.edu Organization: U. C. Berkeley References: <1aa7sjINN2rj@agate.berkeley.edu> Distribution: gnu Date: Thu, 1 Oct 1992 00:37:33 GMT Approved: bug-lib-g++@prep.ai.mit.edu Lines: 45