public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tejohnson at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/61343] New: [C++11] Missing default initialization for class with default constructor
Date: Wed, 28 May 2014 13:29:00 -0000	[thread overview]
Message-ID: <bug-61343-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61343

            Bug ID: 61343
           Summary: [C++11] Missing default initialization for class with
                    default constructor
           Product: gcc
           Version: 4.10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tejohnson at google dot com

The following test case does not call the default constructor when expected:

/////////////////////////////
#include <iostream>

struct Foo {
  int value;

  Foo() noexcept {
    std::cout << this << " constructed. Setting value to twelve.\n";
    value = 12;
  }
};

static thread_local Foo a{};
static thread_local Foo b;

static __attribute__((noinline)) void UseA() {
  const int value = a.value;
  std::cout << "Value of A: " << value << "\n";
}

static __attribute__((noinline)) void UseB() {
  const int value = b.value;
  std::cout << "Value of B: " << value << "\n";
}

int main(int argc, char** argv) {
  // std::cout << "Address of A: " << &a << "\n";
  // std::cout << "Address of B: " << &b << "\n";

  UseA();
  UseA();

  UseB();
  UseB();

  UseA();
  UseA();

  return 0;
}
//////////////////////////

When compiled with the current trunk compiler it gives the output:

>    Value of A: 0
>    Value of A: 0
>    0x7f2bc9150728 constructed. Setting value to twelve.
>    0x7f2bc9150724 constructed. Setting value to twelve.
>    Value of B: 12
>    Value of B: 12
>    Value of A: 12
>    Value of A: 12

I would expect something like:

>    0x7ffa4bc63720 constructed. Setting value to twelve.
>    0x7ffa4bc63724 constructed. Setting value to twelve.
>    Value of A: 12
>    Value of A: 12
>    Value of B: 12
>    Value of B: 12
>    Value of A: 12
>    Value of A: 12

It appears as though the empty brace initialization for 'a', which should
result in the user-defined default constructor being invoked, does not actually
do so. Instead the object is zero-initialized until accessing 'b', which
finally causes the default constructor for 'a' to be run as well.

Google ref: b/15250505


             reply	other threads:[~2014-05-28 13:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-28 13:29 tejohnson at google dot com [this message]
2014-05-28 16:18 ` [Bug c++/61343] " redi at gcc dot gnu.org
2014-06-05 17:31 ` jason at gcc dot gnu.org
2014-06-09 19:29 ` jason at gcc dot gnu.org
2014-06-09 19:29 ` jason at gcc dot gnu.org
2014-06-11 13:45 ` tejohnson at gcc dot gnu.org

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=bug-61343-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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: link
Be 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).