public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, Subject: Re: c++/9881: Incorrect address calculation for static class member Date: Thu, 27 Feb 2003 22:06:00 -0000 [thread overview] Message-ID: <20030227220601.16874.qmail@sources.redhat.com> (raw) The following reply was made to PR c++/9881; it has been noted by GNATS. From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: "Peter A. Buhr" <pabuhr@plg2.math.uwaterloo.ca> Cc: asharji@uwaterloo.ca, <gcc-bugs@gcc.gnu.org>, <gcc-gnats@gcc.gnu.org> Subject: Re: c++/9881: Incorrect address calculation for static class member Date: Thu, 27 Feb 2003 16:05:07 -0600 (CST) > In this code, > foo f; // print output > > double *module::b = &(((bar *)&module::storage)->p); > double module::storage = 0.0; > > The constructor of foo is run before module::b is initialized. > If you change this order, the output is as you expect. > > W. > > http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9881 > > Your response is incorrect. First, the program generates the correct > result with gcc3.2. If you run the example with both gcc3.2 and gcc3.3, > you will see that the output is different. Just for reference: of course I did this. It is not a proof, however, which one is right and which one is wrong. > Hence, one can conclude that > either gcc3.2 or gcc3.3 is wrong. Second, when the example program is > run with the SUN compiler, it generates the same output as for gcc3.2, > indicating that the problem is probably in gcc3.3. One might guess, not conclude. > Third, the position of the constructor has nothing to do > with the assignment to a static variable. The expression to initialize the > static variable "module::b" *MUST* be evaluated at compiler time. All static > variables *MUST* be initialized before any constructor is run because a > constructor can refer to these variables. You are referring to 3.6.2.1, which says that POD type objects with constant initializers need to be initialized before dynamic objects and POD objects with non-constant initializers. Within each group, objects are initialized in the order in which their definitions appear. 5.19 explains what is a constant expression. 5.19.1 talks about constant integral expressions, 5.19.2 about everything else, and refers to 5.19.4 for address constant expressions. 5.19.4 says that casts are allowed, but that the value of an object shall not be accessed. I would claim that this is what the expression ((bar *)&module::storage)->p is doing (note: it is not accessing p, of which we are only taking the address, but it is using the result of the cast; p is not a static member variable). If this claim should be correct, then the initializer would be non-constant, and module::b would have to be initialized after "f", so the constructor of f is run _before_ module::b is initialized. > Finally, your suggestion is not even > a work-around, because the original problem occurred in separate compilation > units, so the notion of moving the constructor in this case does not apply. At which point you are out of luck in any case, since the standard does not give any guarantee about the order of initialization in case there are more than one translation units. May other language lawyers decide this case, regards Wolfgang ------------------------------------------------------------------------- Wolfgang Bangerth email: bangerth@ticam.utexas.edu www: http://www.ticam.utexas.edu/~bangerth/
next reply other threads:[~2003-02-27 22:06 UTC|newest] Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top 2003-02-27 22:06 Wolfgang Bangerth [this message] -- strict thread matches above, loose matches on Subject: below -- 2003-03-02 22:16 Wolfgang Bangerth 2003-02-28 13:56 Peter A. Buhr 2003-02-27 23:16 Wolfgang Bangerth 2003-02-27 22:56 Peter A. Buhr 2003-02-27 21:46 Peter A. Buhr 2003-02-27 21:40 bangerth 2003-02-27 18:22 bangerth 2003-02-27 18:06 asharji
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=20030227220601.16874.qmail@sources.redhat.com \ --to=bangerth@ticam.utexas.edu \ --cc=gcc-prs@gcc.gnu.org \ --cc=nobody@gcc.gnu.org \ /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).