public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/2278: Incorrect executable generated by compiler
@ 2001-04-01  0:00 tomas.szeredi
  0 siblings, 0 replies; only message in thread
From: tomas.szeredi @ 2001-04-01  0:00 UTC (permalink / raw)
  To: gcc-gnats; +Cc: pascal.proulx

>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:


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2001-04-01  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-01  0:00 c++/2278: Incorrect executable generated by compiler tomas.szeredi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).