c++/982: c++ compiler over agressive optimizatoin

alfonso@malibunetworks.com alfonso@malibunetworks.com
Mon Dec 4 14:46:00 GMT 2000


>Number:         982
>Category:       c++
>Synopsis:       c++ compiler over agressive optimizatoin
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 04 14:46:01 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Alfonso Urdaneta
>Release:        gcc version 2.96 20000731 (Red Hat Linux 7.0)
>Organization:
>Environment:
Red Hat Linux 7.0	
>Description:
 Given the following code:
  
 ------------- TestSing.cc ------------
 #include <stdio.h>
 #include "TestSing.hh"
  
 TestSing* TestSing::_instance =0;
 TestSing *TestSing::getInstance()
 {
     if( _instance == 0 )
     {
         printf("creating a new instance\n");
         _instance = new TestSing();
     }
     return _instance;
 }
  
 void TestSing::doStuff()
 {
     printf("doStuff()\n" );
 }
  
 TestSing::TestSing() {}
  
 void main(int argc, char *argv[])
 {
     (TestSing::getInstance())->doStuff();
 }
  
 ----------------- TestSing.hh -------------
 #ifndef TESTSING
 #define TESTSING
 class TestSing
 {
 public:
     static TestSing* getInstance();
     void doStuff();
  
 protected:
     TestSing();
  
 private:
     static TestSing* _instance;
 };
 #endif
 ---------------------------------------

Now when I run this code, I get the following, which is as expected. 
  
 smallpox% a.out
 creating a new instance
 doStuff()
  
 However, if I make doStuff a static function, then getInstance() is never called.
  
 smallpox% a.out
 doStuff()

This is clearly not correct behaviour.
>How-To-Repeat:
See description	
>Fix:
Change the code to read:

TestSing *ts = new TestSing::getInstance();
ts->doStuff();
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the Gcc-prs mailing list