From: Jakub Jelinek <jakub@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Richard Biener <rguenther@suse.de>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] Avoid default-initializing auto_vec<T, N> storage
Date: Fri, 24 Feb 2023 10:55:07 +0100 [thread overview]
Message-ID: <Y/iJe+FSeKAwef5C@tucnak> (raw)
In-Reply-To: <CACb0b4ncRb0omK65R3Aqw-_zAB_QRwMjSY-xxjirBx3Wz4wVZw@mail.gmail.com>
On Fri, Feb 24, 2023 at 09:50:33AM +0000, Jonathan Wakely wrote:
> On Fri, 24 Feb 2023 at 09:49, Jakub Jelinek wrote:
> >
> > Assuming a compiler handles the T m_vecdata[1]; as flexible array member
> > like (which we need because standard C++ doesn't have flexible array members
> > nor [0] arrays), I wonder if we instead of the m_auto followed by m_data
> > trick couldn't make auto_vec have
> > alignas(vec<vl_embed>) unsigned char buf m_data[sizeof (vec<vl_embed>) + (N - 1) * sizeof (T)];
> > and do a placement new of vec<vl_embed> into that m_data during auto_vec
> > construction. Isn't it then similar to how are flexible array members
> > normally used in C, where one uses malloc or alloca to allocate storage
> > for them and the storage can be larger than the structure itself and
> > flexible array member then can use storage after it?
>
> You would still be accessing past the end of the
> vec<vl_embed>::m_vecdata array which is UB.
Pedantically sure, but because C++ doesn't have flexible array members,
people in the wild use the flexible array member like arrays for that
purpose.
If there was T m_vecdata[];, would it still be UB (with the flexible
array member extensions)?
We could use T m_vecdata[]; if the host compiler supports them and
T m_vecdata[1]; otherwise in the hope that the compiler handles it
similarly. After all, I think lots of other real-world programs do the
same.
Jakub
next prev parent reply other threads:[~2023-02-24 9:55 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-23 12:54 Richard Biener
2023-02-23 13:56 ` Jakub Jelinek
2023-02-23 15:02 ` Richard Biener
2023-02-23 15:20 ` Jakub Jelinek
2023-02-24 9:02 ` Richard Biener
2023-02-24 9:34 ` Richard Biener
2023-02-24 9:48 ` Jakub Jelinek
2023-02-24 9:50 ` Jonathan Wakely
2023-02-24 9:55 ` Jakub Jelinek [this message]
2023-02-24 9:55 ` Jonathan Wakely
2023-02-24 10:02 ` Jakub Jelinek
2023-02-24 10:24 ` Jakub Jelinek
2023-02-24 10:30 ` Jonathan Wakely
2023-02-24 10:59 ` Jakub Jelinek
2023-02-24 11:04 ` Jakub Jelinek
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=Y/iJe+FSeKAwef5C@tucnak \
--to=jakub@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jwakely@redhat.com \
--cc=rguenther@suse.de \
/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).