public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: tomas.szeredi@cdott.com To: gcc-gnats@gcc.gnu.org Cc: pascal.proulx@cdott.com Subject: c++/2278: Incorrect executable generated by compiler Date: Sun, 01 Apr 2001 00:00:00 -0000 [thread overview] Message-ID: <20010313183548.31134.qmail@sourceware.cygnus.com> (raw) >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:
reply other threads:[~2001-04-01 0:00 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20010313183548.31134.qmail@sourceware.cygnus.com \ --to=tomas.szeredi@cdott.com \ --cc=gcc-gnats@gcc.gnu.org \ --cc=pascal.proulx@cdott.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).