home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / g / bug / 2333 < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.1 KB  |  103 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!zabriskie.berkeley.edu!truman
  2. From: truman@zabriskie.berkeley.edu
  3. Newsgroups: gnu.g++.bug
  4. Subject: Bug: Temporaries destroyed prematurely
  5. Date: 25 Jan 1993 21:16:48 -0500
  6. Organization: GNUs Not Usenet
  7. Lines: 90
  8. Sender: daemon@cis.ohio-state.edu
  9. Approved: bug-g++@prep.ai.mit.edu
  10. Distribution: gnu
  11. Message-ID: <9301251958.AA09458@heavenly.berkeley.edu.BroderSuns>
  12.  
  13. I believe I have found a bug in the way g++ handles destruction of temporaries.
  14. Below, is the code and the output obtained by running the program.
  15.  
  16. Notice in the 3rd output line, the sentence has been truncated -- what
  17. it looks like to me is that whenever a typecast is used on a temporary
  18. object, the destructor is for the object is involed prematurely.  In
  19. my code (below), the line
  20.  
  21. example C( (const char*) B.PrintName() );
  22.  
  23. should do some like this:
  24.     1] call B.PrintName(), which returns a String temporary
  25.     2] call String::operator char*() on the temporary
  26.     3] invoke example::example(const char*)
  27.     4] call the destructor for the temporary
  28.  
  29. however, what actually happens is that the 4th and 3rd step become interchanged.
  30.  
  31. Here is the output of compilation:
  32.  
  33. >>>>>
  34. yosemite[149]> g++ -v -ggdb test.cc
  35. Reading specs from /users/truman/gnu/lib/gcc-lib/sparc-sun-sunos4.1/2.3.3/specs
  36. gcc version 2.3.3
  37.  /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
  38.  GNU CPP version 2.3.3 (sparc)
  39.   /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
  40.   GNU C++ version 2.3.3 (sparc) compiled by GNU C version 2.3.3.
  41.    as -o /usr/tmp/cca043181.o /usr/tmp/cca04318.s
  42.     /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
  43.  
  44. >>>>>>>>>>>>
  45.  
  46. here is the program output:
  47.  
  48. yosemite[150]> a.out
  49. A.name = one-long-testing-name-
  50. B.name = one-long-testing-name-
  51.  
  52. C.name = one-long-testing-nam
  53.                             ^ 
  54. ____________________________|
  55.  
  56. note the truncation
  57.  
  58. >>>>>>>>>>>>>>
  59.  
  60. //
  61. // test.cc
  62. //
  63. //
  64.  
  65. #include <String.h>
  66. #include <iostream.h>
  67. #include <fstream.h>
  68. #include <stido.h>
  69.  
  70.  
  71. class example
  72. {
  73.     char*    name;
  74.  
  75. public:
  76.     example(const char* n)     { name = new char[ strlen(n) + 1];
  77.                   strcpy(name,n);
  78.                 }
  79.  
  80.     example(String& s)        { name = new char[ s.length() + 1];
  81.                   strcpy(name,(char*)s);
  82.                 }
  83.  
  84.     String PrintName()        { return String(name); }
  85.  
  86.     ~example()            { delete name; }
  87.  
  88. };
  89.  
  90. main()
  91. {
  92.     example A("one-long-testing-name-");
  93.     example B( A.PrintName() );
  94.     example C( (const char*)B.PrintName() );
  95.  
  96.     cout << "A.name = " << A.PrintName();
  97.     cout << "\nB.name = " << B.PrintName() << "\n";
  98.     cout << "\nC.name = " << C.PrintName() << "\n";
  99.  
  100. }
  101.  
  102.  
  103.