[RFA 3/3] Remove cleanups from break-catch-syscall.c

Pedro Alves palves@redhat.com
Wed Oct 25 10:38:00 GMT 2017


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



More information about the Gdb-patches mailing list