From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19963 invoked by alias); 25 Oct 2017 10:38:36 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 19950 invoked by uid 89); 25 Oct 2017 10:38:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=no version=3.3.2 spammy=Hx-languages-length:1612 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 25 Oct 2017 10:38:34 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 82A214E33A; Wed, 25 Oct 2017 10:38:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 82A214E33A Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=palves@redhat.com Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id D7FD76FA83; Wed, 25 Oct 2017 10:38:31 +0000 (UTC) Subject: Re: [RFA 3/3] Remove cleanups from break-catch-syscall.c To: Tom Tromey , Simon Marchi References: <20171018040645.7212-1-tom@tromey.com> <20171018040645.7212-3-tom@tromey.com> <280a7994-38a6-2138-b199-8902ee2651b0@ericsson.com> <87tvynrg8h.fsf@tromey.com> Cc: gdb-patches@sourceware.org From: Pedro Alves Message-ID: Date: Wed, 25 Oct 2017 10:38:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <87tvynrg8h.fsf@tromey.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-10/txt/msg00765.txt.bz2 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