public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gene at staubsaal dot de" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-115180-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            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=c++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 going
on?)

```
#include <cassert>
#include <vector>


// Since K+1 the returned vector has size() >= 1
inline auto f(size_t N, size_t K) {
    return std::vector<std::vector<size_t>>(K+1, std::vector<size_t>(N, 0));
}

// create another vector, but derive size from the previous vector
inline auto g(std::vector<std::vector<size_t>> const& v1) {
    size_t const K = v1.size() - 1;
    size_t const N = v1[0].size();
    return std::vector<std::vector<size_t>>(K+1, std::vector<size_t>(N, 0));
}

struct S {
    std::vector<size_t> v1;
    std::vector<size_t> v2;
    // v1, v2always have the same length
};

auto h(size_t N, size_t K) {
    assert(N>0);
    assert(N >= K);

    auto v1 = f(N, K);
    auto v2 = g(v1);

    auto ss = std::vector<S>{};
    for (size_t i{0}; i < v1.size(); ++i) {
        ss.emplace_back(S{v1[i], {}});
    }

    for (auto& s : ss) {
        s.v1.back() += 1;
    }

    return ss;
}

auto all = h(4, 3);
```
For convenience a link to godbolt: https://godbolt.org/z/dPj1YYf7x

             reply	other threads:[~2024-05-21 18:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-21 18:11 gene at staubsaal dot de [this message]
2024-05-21 18:15 ` [Bug c++/115180] " gene at staubsaal dot de

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