unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Vladimir Marek <Vladimir.Marek@Oracle.COM>
To: Kim Minh Kaplan <kimminh.kaplan+nomuch@afnic.fr>
Cc: Tomi Ollila <tomi.ollila@iki.fi>,
	notmuch@notmuchmail.org, Vladimir Marek <vlmarek@volny.cz>
Subject: Re: [PATCH] don't store temporary value returned from c_str()
Date: Tue, 30 Apr 2013 10:48:52 +0200	[thread overview]
Message-ID: <20130430084852.GA19599@pub.cz.oracle.com> (raw)
In-Reply-To: <8761z4h7ih.fsf@kaki.tech.prive.nic.fr>

[-- Attachment #1: Type: text/plain, Size: 1331 bytes --]

Hi,

Thank you, I found it eventually too. But I wrote little test program
(attached) which confused me. I haven't had much time to take a look
into it since weekend.

The idea is to have temporary object where I can detect whether
destructor was called.

I thought that

printf ("%s\n", s.c_str());
will print "test"

and

x=s.c_str();
printf ("%s\n", x);

will print "destroyed"

On my machine both prints "destroyed".


I still believe my fix is correct, but I'm not at the position to be
able to defend it at the moment :)

Thank you
-- 
	Vlad

> The january 2012 working draft: 
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
> 
>     12.2 Temporary objects [class.temporary]
> 
>     1 Temporaries of class type are created in various contexts: binding a
>     reference to a prvalue (8.5.3), returning a prvalue (6.6.3) […]
> 
>     3 When an implementation introduces a temporary object of a class that
>     has a non-trivial constructor (12.1, 12.8), it shall ensure that a
>     constructor is called for the temporary object. Similarly, the
>     destructor shall be called for a temporary with a non-trivial destructor
>     (12.4). Temporary objects are destroyed as the last step in evaluating
>     the full-expression (1.9) that (lexically) contains the point where they
>     were created.

[-- Attachment #2: a.cc --]
[-- Type: text/plain, Size: 603 bytes --]

#include <stdio.h>
#include <vector>
#include <string.h>

using namespace std;

class array {
	vector<char> impl;

	public:

	array(int size):impl(size) { }
	array(array &in):impl(in.impl) { }

	char*
	operator[](size_t i) {
		return &impl[i];
	};

	~array() {
		strcpy(&impl[0], "destroyed");
	}
};

class str {
	array tmp;

	public:

	str(const char *input):tmp(100) {
		strcpy(tmp[0], input);
	};

	const char* c_str() {
		return (array(tmp))[0];
	};
};


int
main (int argc, char **argv)
{
	str s("test");
	const char *x;
	printf ("%s\n", s.c_str());
	x=s.c_str();
	printf ("%s\n", x);
	return 0;
}

  reply	other threads:[~2013-04-30  8:51 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-19 21:12 [PATCH] don't store temporary value returned from c_str() Vladimir.Marek
2013-04-27  9:33 ` Tomi Ollila
2013-04-27 10:11   ` Vladimir Marek
2013-04-27 11:53     ` David Bremner
2013-04-27 15:59       ` Vladimir Marek
2013-04-30  6:12     ` Kim Minh Kaplan
2013-04-30  8:48       ` Vladimir Marek [this message]
2013-04-30  9:44         ` Kim Minh Kaplan
2013-05-01 11:28           ` Vladimir Marek
2013-04-27 12:30 ` Jani Nikula
2013-04-27 13:22   ` Tomi Ollila
2013-04-27 16:05   ` Vladimir Marek
2013-04-29 21:23     ` David Bremner
2013-05-01 21:33       ` Vladimir Marek

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

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130430084852.GA19599@pub.cz.oracle.com \
    --to=vladimir.marek@oracle.com \
    --cc=kimminh.kaplan+nomuch@afnic.fr \
    --cc=notmuch@notmuchmail.org \
    --cc=tomi.ollila@iki.fi \
    --cc=vlmarek@volny.cz \
    /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.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

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).