This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/2278: Incorrect executable generated by compiler



>Number:         2278
>Category:       c++
>Synopsis:       Incorrect executable generated by compiler
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 13 10:36:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Tom Szeredi
>Release:        gcc version 2.95.1 19990816 (release)
>Organization:
>Environment:
sun4 sparc solaris 
>Description:
There seems to be a bug with gcc and certain classes (singletons) which have static methods and only static class variables.

This bug does not happen with Sun CC.

The following class has a single (non-static) class variable called "value". Inside the constructor
it is initialized to 1. Inside the destructor it is set to -10.  There is one static function called getValue
which returns "value". The main program calls this function and prints the result (which should be 
1, since value is set to 1 in the constructor).


#include <iostream.h>

class singleton
{ 
public:

  singleton() 
  { 
    value = 1;
    cout << "Inside constructor" << endl;
    cout << "value = " << value << endl;
  }

  ~singleton()
  {
    value = -10;
    cout << "Inside destructor" << endl;
    cout << "value = " << value << endl;
  }

  static int getValue()
  {
    return(instance().value);
  }

private:
  
  int value;

  static singleton& instance()
  {
    static singleton a;
    return(a);
  }
};

int main()
{
  int myValue = 0;

  myValue = singleton::getValue();

  cout << "Inside Main. myValue = " << myValue << endl;

  return 0;

}

The output from running this code is

Inside constructor
value = 1
Inside Main. myValue = 1
Inside destructor
value = -10

This is the correct behaviour.

However, if you define "value" as "static", as in the code below, then the constructor and destructor
are never invoked! Something very strange goes on (I'm not sure what), but somehow the "value" class
variable is accessed in main (via the singleton class) but the singleton class' constructor and destructor
are never invoked. 

#include <iostream.h>

class singleton
{ 
public:

  singleton() 
  { 
    value = 1;
    cout << "Inside constructor" << endl;
    cout << "value = " << value << endl;
  }

  ~singleton()
  {
    value = -10;
    cout << "Inside destructor" << endl;
    cout << "value = " << value << endl;
  }

  static int getValue()
  {
    return(instance().value);
  }

private:
  
  static int value;

  static singleton& instance()
  {
    static singleton a;
    return(a);
  }
};

int singleton::value = -20;

int main()
{
  int myValue = 0;

  myValue = singleton::getValue();

  cout << "Inside Main. myValue = " << myValue << endl;

  return 0;

}


The output from running this program is:

Inside Main. myValue = -20

which is incorrect. "value" is never reset by the constructor nor by the destructor. 
Neither the constructor nor the destructor are ever entered!
>How-To-Repeat:
Compile and run the two code snippets included in the description using
gcc -stdc++ whatever_you_name_the_code_snippets

Then run the executables.

The code snipped with "value" static will have incorrect 
behaviour.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]