Thread View: gnu.g++.lib.bug
2 messages
2 total messages
Started by Wayne A. Christopher
Tue, 29 Sep 1992 18:39
ofstream bug in 2.1
Author: Wayne A. Christopher
Date: Tue, 29 Sep 1992 18:39
Date: Tue, 29 Sep 1992 18:39
33 lines
1293 bytes
1293 bytes
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.
foo.cc:
#include <fstream.h>
static ofstream ofile;
main()
{
ofile = ofstream("outputfile");
ofile << "Here is some output\n";
}
What happens:
% gcc-2.2.2 foo.cc -lg++-2_1
% gdb a.out
(gdb) run
Program received signal 6, Abort
0xf775f458 in kill ()
(gdb) where
#0 0xf775f458 in kill ()
#1 0xf77776fc in abort ()
#2 0xb93c in __overflow (sb=0x16650, c=72) at streambuf.C:186
#3 0xba4c in streambuf::sputn (this=0x16650, s=0x22a1 "ere is some output\n",
n=20) at streambuf.C:211
#4 0x5aa0 in operator<< (stream=@0x145b0, s=0x22a0 "Here is some output\n")
at iostream.C:525
#5 0x2358 in main ()
Re: ofstream bug in 2.1
Author: 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
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