From: Pedro Alves <palves@redhat.com>
To: Tom Tromey <tom@tromey.com>, Simon Marchi <simon.marchi@ericsson.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [RFA 3/3] Remove cleanups from break-catch-syscall.c
Date: Wed, 25 Oct 2017 10:38:00 -0000 [thread overview]
Message-ID: <b20af1f8-5d5b-2136-739a-88bed545b2ea@redhat.com> (raw)
In-Reply-To: <87tvynrg8h.fsf@tromey.com>
On 10/25/2017 05:41 AM, Tom Tromey wrote:
> Simon> It is not enough to assign holders.back ().c_str () (after having pushed the string in
> Simon> the vector), because when the vector gets reallocated it can now point to stale memory.
> Simon> I think we have to do it in two pass, prepare the vector of std::string, and then get
> Simon> pointers to the strings.
>
> Sorry about that. I think I considered this after the earlier review
> and believed that the resizing wouldn't affect the locations; my
> thinking being that growing the vector must surely use the move
> constructor to move the strings into place -- since isn't this pretty
> much the whole reason rvalues even exist? But perhaps this doesn't
> happen sometimes for some reason.
std::vector will copy instead of move on resize if:
- the element type is copyable and,
- the move ctor is not marked noexcept.
This has to do with strong/basic exception guarantees.
But in this case the element type is a std::string, and
std::string can (and most implementations do) make use of
the SSO (small string optimization). I.e., very simplified,
something like:
string
{
struct free_store_data
{
char *ptr;
size_t capacity;
};
union
{
char internal_buf[sizeof (free_store_data)];
free_store_data free_store_buf;
} m_data;
size_t m_size;
};
When the string data fits in the internal buffer, moving the
object must mean copying the internal buffer
and in that case "moved_from.c_str()" will still be left
pointing to the internal buffer of the moved-from
std::string.
Thanks,
Pedro Alves
next prev parent reply other threads:[~2017-10-25 10:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-18 4:06 [RFA 1/3] Remove cleanups from prepare_execute_command Tom Tromey
2017-10-18 4:06 ` [RFA 2/3] Remove cleanup from call_function_by_hand_dummy Tom Tromey
2017-10-18 4:06 ` [RFA 3/3] Remove cleanups from break-catch-syscall.c Tom Tromey
2017-10-19 21:00 ` Simon Marchi
2017-10-19 21:57 ` Tom Tromey
2017-10-19 21:57 ` Tom Tromey
2017-10-20 20:26 ` Simon Marchi
2017-10-25 4:42 ` Tom Tromey
2017-10-25 10:38 ` Pedro Alves [this message]
2017-10-25 14:50 ` Tom Tromey
2017-12-06 18:58 ` Sergio Durigan Junior
2017-12-06 21:41 ` Simon Marchi
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=b20af1f8-5d5b-2136-739a-88bed545b2ea@redhat.com \
--to=palves@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@ericsson.com \
--cc=tom@tromey.com \
/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