home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16131 < prev    next >
Encoding:
Text File  |  1992-11-11  |  2.1 KB  |  67 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!iggy.GW.Vitalink.COM!cs.widener.edu!eff!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!swrinde!sdd.hp.com!hp-cv!hp-pcd!hp-vcd!nuntius
  3. From: John Matthews <jm@vcd.hp.com>
  4. Subject: MPW C++ bug?
  5. Sender: news@vcd.hp.com (News user)
  6. Message-ID: <BxKz81.8C5@vcd.hp.com>
  7. X-Useragent: Nuntius v1.1a5
  8. Date: Thu, 12 Nov 1992 02:07:13 GMT
  9. Organization: Hewlett-Packard
  10. Lines: 55
  11.  
  12. I seem to have run into a possible bug between the C++ compiler and the
  13. MPW linker.
  14. The problem concerns statically (globally) allocated objects with
  15. constructors.
  16. If the global object is not referenced anywhere else in the program, but
  17. does have
  18. a constructor that causes side-effects, I believe the semantics of C++
  19. states that
  20. that object must be constructed.
  21.  
  22. However, since the linker notices that no-one is actually using the
  23. global variable,
  24. it strips out the object, along with its constructor call, from the
  25. program.
  26.  
  27. Although the code that ran into this problem is complicated, here is an
  28. example
  29. of the kind of problem I've encountered:
  30.  
  31.               int gGlobal = 0; // Used by main()
  32.  
  33.               main()
  34.               {
  35.                     // Should print out: "gGlobal = 1",
  36.                     cout << "gGlobal = " << gGlobal << '\n'; 
  37.               }
  38.  
  39.               class StaticClass
  40.               {
  41.               public:
  42.                          StaticClass();
  43.               };
  44.  
  45.               StaticClass::StaticClass()
  46.               {
  47.                         gGlobal = 1;
  48.               }
  49.  
  50.               // Global declaration
  51.               StaticClass gStrippedOutByLinker;
  52.  
  53. If I were to build this  program, neither gStrippedOutByLinker nor 
  54. StaticClass::StaticClass() would be linked into the final program.
  55. However, if I add the following line as the last statement in the main()
  56. routine,
  57. everything works as expected (gGlobal == 1).
  58.  
  59. I have created a class whose sole purpose is to have a
  60. statically-allocated
  61. constructor perform a useful action WITHOUT having to reference the
  62. variable
  63. anywhere in the program. I don't see any way I can currently achieve this.
  64.  
  65. John Matthews
  66. Hewlett-Packard
  67.