home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: John Matthews <jm@vcd.hp.com>
- Subject: MPW C++ bug?
- Sender: news@vcd.hp.com (News user)
- Message-ID: <BxKz81.8C5@vcd.hp.com>
- X-Useragent: Nuntius v1.1a5
- Date: Thu, 12 Nov 1992 02:07:13 GMT
- Organization: Hewlett-Packard
- Lines: 55
-
- I seem to have run into a possible bug between the C++ compiler and the
- MPW linker.
- The problem concerns statically (globally) allocated objects with
- constructors.
- If the global object is not referenced anywhere else in the program, but
- does have
- a constructor that causes side-effects, I believe the semantics of C++
- states that
- that object must be constructed.
-
- However, since the linker notices that no-one is actually using the
- global variable,
- it strips out the object, along with its constructor call, from the
- program.
-
- Although the code that ran into this problem is complicated, here is an
- example
- of the kind of problem I've encountered:
-
- int gGlobal = 0; // Used by main()
-
- main()
- {
- // Should print out: "gGlobal = 1",
- cout << "gGlobal = " << gGlobal << '\n';
- }
-
- class StaticClass
- {
- public:
- StaticClass();
- };
-
- StaticClass::StaticClass()
- {
- gGlobal = 1;
- }
-
- // Global declaration
- StaticClass gStrippedOutByLinker;
-
- If I were to build this program, neither gStrippedOutByLinker nor
- StaticClass::StaticClass() would be linked into the final program.
- However, if I add the following line as the last statement in the main()
- routine,
- everything works as expected (gGlobal == 1).
-
- I have created a class whose sole purpose is to have a
- statically-allocated
- constructor perform a useful action WITHOUT having to reference the
- variable
- anywhere in the program. I don't see any way I can currently achieve this.
-
- John Matthews
- Hewlett-Packard
-