Thread View: gnu.g++.bug
1 messages
1 total messages
Started by truman
Tue, 26 Jan 1993 02:16
Bug: Temporaries destroyed prematurely
Author: truman
Date: Tue, 26 Jan 1993 02:16
Date: Tue, 26 Jan 1993 02:16
88 lines
3239 bytes
3239 bytes
I believe I have found a bug in the way g++ handles destruction of temporaries.
Below, is the code and the output obtained by running the program.
Notice in the 3rd output line, the sentence has been truncated -- what
it looks like to me is that whenever a typecast is used on a temporary
object, the destructor is for the object is involed prematurely. In
my code (below), the line
example C( (const char*) B.PrintName() );
should do some like this:
1] call B.PrintName(), which returns a String temporary
2] call String::operator char*() on the temporary
3] invoke example::example(const char*)
4] call the destructor for the temporary
however, what actually happens is that the 4th and 3rd step become interchanged.
Here is the output of compilation:
>>>>>
yosemite[149]> g++ -v -ggdb test.cc
Reading specs from /users/truman/gnu/lib/gcc-lib/sparc-sun-sunos4.1/2.3.3/specs
gcc version 2.3.3
/users/truman/gnu/bin/cpp -lang-c++ -v -iprefix /users/truman/gnu/bin/sparc-sun-sunos4.1/2.3.3/ -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix -ggdb test.cc /usr/tmp/cca04318.i
GNU CPP version 2.3.3 (sparc)
/users/truman/gnu/lib/gcc-lib/sparc-sun-sunos4.1/2.3.3/cc1plus /usr/tmp/cca04318.i -quiet -dumpbase test.cc -ggdb -version -o /usr/tmp/cca04318.s
GNU C++ version 2.3.3 (sparc) compiled by GNU C version 2.3.3.
as -o /usr/tmp/cca043181.o /usr/tmp/cca04318.s
/users/truman/gnu/bin/ld -e start -dc -dp /lib/crt0.o -L/users/truman/gnu/lib -L/users/truman/gnu/bin -L/users/truman/gnu/lib -L/users/truman/gnu/lib/gcc-lib/sparc-sun-sunos4.1/2.3.3 -L/users/truman/gnu/lib /usr/tmp/cca043181.o -lg++ -lgcc -lc -lgcc
>>>>>>>>>>>>
here is the program output:
yosemite[150]> a.out
A.name = one-long-testing-name-
B.name = one-long-testing-name-
C.name = one-long-testing-nam
^
____________________________|
note the truncation
>>>>>>>>>>>>>>
//
// test.cc
//
//
#include <String.h>
#include <iostream.h>
#include <fstream.h>
#include <stido.h>
class example
{
char* name;
public:
example(const char* n) { name = new char[ strlen(n) + 1];
strcpy(name,n);
}
example(String& s) { name = new char[ s.length() + 1];
strcpy(name,(char*)s);
}
String PrintName() { return String(name); }
~example() { delete name; }
};
main()
{
example A("one-long-testing-name-");
example B( A.PrintName() );
example C( (const char*)B.PrintName() );
cout << "A.name = " << A.PrintName();
cout << "\nB.name = " << B.PrintName() << "\n";
cout << "\nC.name = " << C.PrintName() << "\n";
}
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