From 521ec5b5fe327207bc629cfefcde2ad124d1da39 Mon Sep 17 00:00:00 2001 From: Vladimir Marek Date: Wed, 1 May 2013 13:28:06 +0200 Subject: [PATCH] Re: [PATCH] don't store temporary value returned from c_str() --- 0c/12be5225cd5f5babaf8af6920cbb445140bc85 | 115 ++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 0c/12be5225cd5f5babaf8af6920cbb445140bc85 diff --git a/0c/12be5225cd5f5babaf8af6920cbb445140bc85 b/0c/12be5225cd5f5babaf8af6920cbb445140bc85 new file mode 100644 index 000000000..35908bc5d --- /dev/null +++ b/0c/12be5225cd5f5babaf8af6920cbb445140bc85 @@ -0,0 +1,115 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 64D8E431FAF + for ; Wed, 1 May 2013 04:30:27 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.299 +X-Spam-Level: +X-Spam-Status: No, score=-2.299 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3, UNPARSEABLE_RELAY=0.001] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id lB+-6ahJ-ypu for ; + Wed, 1 May 2013 04:30:21 -0700 (PDT) +Received: from aserp1040.oracle.com (aserp1040.oracle.com [141.146.126.69]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 309DA431FC0 + for ; Wed, 1 May 2013 04:30:20 -0700 (PDT) +Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) + by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with + ESMTP id r41BU6K2011375 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); + Wed, 1 May 2013 11:30:07 GMT +Received: from aserz7021.oracle.com (aserz7021.oracle.com [141.146.126.230]) + by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r41BU4b0003921 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); + Wed, 1 May 2013 11:30:04 GMT +Received: from abhmt115.oracle.com (abhmt115.oracle.com [141.146.116.67]) + by aserz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r41BU36E003914; Wed, 1 May 2013 11:30:03 GMT +Received: from pub.cz.oracle.com (/10.163.101.122) + by default (Oracle Beehive Gateway v4.0) + with ESMTP ; Wed, 01 May 2013 04:30:03 -0700 +Date: Wed, 1 May 2013 13:28:06 +0200 +From: Vladimir Marek +To: Kim Minh Kaplan +Subject: Re: [PATCH] don't store temporary value returned from c_str() +Message-ID: <20130501112806.GD19599@pub.cz.oracle.com> +Mail-Followup-To: Kim Minh Kaplan , + Vladimir Marek , Tomi Ollila , + notmuch@notmuchmail.org +References: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> + + <20130427101134.GI10394@pub.cz.oracle.com> + <8761z4h7ih.fsf@kaki.tech.prive.nic.fr> + <20130430084852.GA19599@pub.cz.oracle.com> + <87ehdsfj4u.fsf@kaki.tech.prive.nic.fr> +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Disposition: inline +In-Reply-To: <87ehdsfj4u.fsf@kaki.tech.prive.nic.fr> +User-Agent: Mutt/1.5.21 (2010-09-15) +X-Source-IP: acsinet21.oracle.com [141.146.126.237] +Cc: Tomi Ollila , notmuch@notmuchmail.org, + Vladimir Marek +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 01 May 2013 11:30:27 -0000 + +> You have to somehow instantiate a temporary object, using a function +> call for example. +> +> #include +> #include +> struct example { +> char data[10]; +> char *c_str() { return data; } +> example() { strcpy(data, "test"); } +> ~example() { strcpy(data, "destroyed"); } +> }; +> example foo() +> { +> example res; +> return res; +> } +> main() +> { +> printf("%s\n", foo().c_str()); +> char *x = foo().c_str(); +> printf("%s\n", x); +> } + +Right. After more tests I think I'm getting hang of it. In my examples I +had function similar to + +char *foo() { + example res; + return res.c_str(); +} + +In this case the reference returned is to _char pointer_ and not to +example struct. So the struct is destroyed before returning from foo to +caller. Compiler does not understand that the returned char pointer does +not have any meaning without the example struct. + +This is where I was doing the mistake, I believe. + +Thank you +-- + Vlad -- 2.26.2