public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "morwenn29 at hotmail dot fr" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/63717] New: Value-initialization performs defaut-initialization when =default is outside the class declaration
Date: Sun, 02 Nov 2014 22:50:00 -0000	[thread overview]
Message-ID: <bug-63717-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 63717
           Summary: Value-initialization performs defaut-initialization
                    when =default is outside the class declaration
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: morwenn29 at hotmail dot fr

I have this minimal test case:

    struct foo
    {
        unsigned value;

        foo();
        foo(unsigned value):
            value(value)
        {}
    };

    foo::foo()
        = default;

    int main()
    {
        foo bar{};
    }

In this test case, foo bar{}; should zero-initialize bar and thus
zero-initialize value (see http://stackoverflow.com/q/26699720/1364752 for a
discussion about that). However, value is sometimes initialized with garbage
instead of being initialized with 0. Here is the assembly produced by the
example above:

    push   %ebp
    mov    %esp,%ebp
    and    $0xfffffff0,%esp
    sub    $0x10,%esp
    call   0x402060 <__main>
    lea    0xc(%esp),%eax
    mov    %eax,%ecx
    call   0x401610 <foo::foo()>
    mov    $0x0,%eax
    leave
    ret

As you can see, the default constructor of foo is called, but value is not
zero-initialized here. However, if we move = default in the class declaration:

    struct foo
    {
        unsigned value;

        foo() = default;
        foo(unsigned value):
            value(value)
        {}
    };

    foo::foo()
        = default;

    int main()
    {
        foo bar{};
    }

Then, the produced assembly is not the same anymore and value is properly
zero-initialized as expected:

    push   %ebp
    mov    %esp,%ebp
    and    $0xfffffff0,%esp
    sub    $0x10,%esp
    call   0x402050 <__main>
    movl   $0x0,0xc(%esp) ; init value (?)
    mov    $0x0,%eax
    leave
    ret

I am not really good at reading assembly but I fixed my problem by moving the
=default into the class instead separating the declaration of the default
constructor and its definition. If I correctly read both the standard and the
assembly, I am pretty sure that this is a GCC bug.


             reply	other threads:[~2014-11-02 22:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-02 22:50 morwenn29 at hotmail dot fr [this message]
2014-11-03  0:58 ` [Bug c++/63717] " morwenn29 at hotmail dot fr
2014-11-03  9:16 ` morwenn29 at hotmail dot fr

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-63717-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).