[PATCH] don't store temporary value returned from c_str()

Vladimir Marek Vladimir.Marek at Oracle.COM
Sat Apr 27 03:11:35 PDT 2013


> > From: Vladimir Marek <vlmarek at volny.cz>
> >
> > This is causing problems when compiled by Oracle Studio. Memory pointed
> > by (const char*)term was already changed once talloc_strdup was called.
> 
> If that changes, I'd like to understand why (and stated in the commit
> message). If that is clear to everyone else I will withdraw the question --
> I am not too familiar with these iterators magic... :D

Well, a) standards says that

A temporary bound to a reference parameter in a function call (5.2.2)
persists until the completion of the full expression containing the call

(you can find the message all over the net, but I can't find actual link
to the standard :-/)


b) Imagine the function c_str() looks like that:

const char*
string::c_str(void) {
	char buf[100];

	strcpy (buf, this->internal_representation);
	return buf;
}

you can't do:
char *my_tmp = string.c_str();
printf(my_tmp);

you can do:
printf(string.c_str());

Hopefully I made the example right ...

Cheers
-- 
	Vlad


More information about the notmuch mailing list