public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "13508417 at qq dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/102573] optimized code removes the underlying elements of the std::initializer_list being copied
Date: Sun, 03 Oct 2021 14:16:59 +0000	[thread overview]
Message-ID: <bug-102573-4-I9iHwe1hAq@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-102573-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #4 from wjf <13508417 at qq dot com> ---
Hi Andrew,

I've updated my gcc to 10.2.1, and find while it did show what you said for my
initial code snippet, it won't warn me anything, nor report error at runtime
with -fsanitize=address (note that clang version 10.0.0 will report error at
runtime however), for below two code snippets:

----

code 1:
#include<iostream>
#include<initializer_list>
using namespace std;

int main()
{
    initializer_list<int> il({111, 222});

    cout << "initializer_list size: " << il.size() << endl;
    cout << "initializer_list underlying elements: " << endl;
    for (auto i : il)
        cout << i << endl;

    return 0;
}

----

code 2:
#include<iostream>
#include<initializer_list>
using namespace std;

int main()
{
    initializer_list<int> il;
    {
        initializer_list<int> il2 = {111, 222};
        il = il2;
    }

    cout << "initializer_list size: " << il.size() << endl;
    cout << "initializer_list underlying elements: " << endl;
    for (auto i : il)
        cout << i << endl;

    return 0;
}

----

As you can see, above two code snippets should still have the similar
"lifespan" issue. code 1's il constructs from an temporary, but doesn't extend
the lifetime of the underlying array. code 2's il is assigned from il2, but
have longer lifetime than il2. When using clang++ optimization, the output will
be something like below:

initializer_list size: 2
initializer_list underlying elements:
1877862544
32767

  parent reply	other threads:[~2021-10-03 14:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-03  4:04 [Bug libstdc++/102573] New: " 13508417 at qq dot com
2021-10-03  4:09 ` [Bug libstdc++/102573] " pinskia at gcc dot gnu.org
2021-10-03  5:49 ` 13508417 at qq dot com
2021-10-03  6:20 ` pinskia at gcc dot gnu.org
2021-10-03 14:16 ` 13508417 at qq dot com [this message]
2021-10-04  6:55 ` redi 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-102573-4-I9iHwe1hAq@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).