🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1421

Bug: Temporaries destroyed prematurely

#1421
From: truman
Date: Tue, 26 Jan 1993 02:16
88 lines
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";

}
Technical Details
Message-ID: <9301251958.AA09458@heavenly.berkeley.edu.BroderSuns>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!zabriskie.berkeley.edu!truman

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!zabriskie.berkeley.edu!truman
From: truman@zabriskie.berkeley.edu
Newsgroups: gnu.g++.bug
Subject: Bug: Temporaries destroyed prematurely
Date: 25 Jan 1993 21:16:48 -0500
Organization: GNUs Not Usenet
Lines: 90
Sender: daemon@cis.ohio-state.edu
Approved: bug-g++@prep.ai.mit.edu
Distribution: gnu
Message-ID: <9301251958.AA09458@heavenly.berkeley.edu.BroderSuns>