From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AF3D93858D1E; Tue, 21 May 2024 18:11:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF3D93858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1716315107; bh=2QsFd+Kc38p9kmvYO+VPchJk5ndSUbpYDkWT9b/vTts=; h=From:To:Subject:Date:From; b=IB/Tm/7BZekn5UUUCAoCMnjj6Hui9g7iXqjsmPz6qc/9awpiJKbjIkfVNk/ILcJbu SuLMC/2iEIRVZu56fbKar44tPDJUNzUZDKgOREwF59ApN/gxThiBsIIbNiiylBcoeJ /9EtqY4q2KPga3XbWmeGYXxcyvhHiALOpt+Hr/kw= From: "gene at staubsaal dot de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/115180] New: [regression] free-nonheap-object on std::vector usage Date: Tue, 21 May 2024 18:11:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gene at staubsaal dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D115180 Bug ID: 115180 Summary: [regression] free-nonheap-object on std::vector usage Product: gcc Version: 14.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gene at staubsaal dot de Target Milestone: --- Compiler: gcc (GCC) 14.1.1 Compile flags: -std=3Dc++20 -O3 -Wfree-nonheap-object The following code produces an "-Wfree-nonheap-object" warning, which doesn= 't happen with '-O2' or using an older version of gcc. This seems to be a false warning. (Or maybe even some false optimization go= ing on?) ``` #include #include // Since K+1 the returned vector has size() >=3D 1 inline auto f(size_t N, size_t K) { return std::vector>(K+1, std::vector(N, 0)); } // create another vector, but derive size from the previous vector inline auto g(std::vector> const& v1) { size_t const K =3D v1.size() - 1; size_t const N =3D v1[0].size(); return std::vector>(K+1, std::vector(N, 0)); } struct S { std::vector v1; std::vector v2; // v1, v2always have the same length }; auto h(size_t N, size_t K) { assert(N>0); assert(N >=3D K); auto v1 =3D f(N, K); auto v2 =3D g(v1); auto ss =3D std::vector{}; for (size_t i{0}; i < v1.size(); ++i) { ss.emplace_back(S{v1[i], {}}); } for (auto& s : ss) { s.v1.back() +=3D 1; } return ss; } auto all =3D h(4, 3); ``` For convenience a link to godbolt: https://godbolt.org/z/dPj1YYf7x=