<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 4/22/2014 11:55 AM, Hanbing Li
wrote:<br>
</div>
<blockquote
cite="mid:1617914309.2480038.1398185704618.JavaMail.zimbra@inria.fr"
type="cite">
<div style="font-family: times new roman, new york, times, serif;
font-size: 12pt; color: #000000">
<div>Hi,</div>
<div><br>
</div>
<div>I tried to get the name of function+loop, so I write a
small function as following:</div>
<div><br>
</div>
<div>
<p style="margin: 0px;" data-mce-style="margin: 0px;">StringRef
getStringofFL(const Function * F, const Loop * L){<br>
StringRef sr;<br>
std::string s1 = F->getName();<br>
std::string s2 = L->getHeader()->getName();<br>
sr = s1+s2;<br>
return sr;<br>
}</p>
<p style="margin: 0px;" data-mce-style="margin: 0px;"><br>
</p>
<p style="margin: 0px;" data-mce-style="margin: 0px;">However,
two questions came:</p>
<p style="margin: 0px;" data-mce-style="margin: 0px;">1, if I
output sr like: errs() << sr; it's ok (for instance: <span
style="font-family: Menlo; font-size: 11px;"
data-mce-style="font-family: Menlo; font-size: 11px;">mainfor.cond</span><span
style="font-size: 12pt;" data-mce-style="font-size: 12pt;">),
but when I called this function and got the
StringRef: StringRef s= getStringofFL(F, L); errs()
<< s; the result is: </span><span
style="font-family: Menlo; font-size: 11px;"
data-mce-style="font-family: Menlo; font-size: 11px;">m\275[\377\177\000\0002\354\333\004\001,<span
style="font-family: arial, helvetica, sans-serif;
font-size: small;"> </span></span><span
style="font-family: arial, helvetica, sans-serif;
font-size: small;">why?</span></p>
</div>
</div>
</blockquote>
<br>
StringRef is basically a wrapper around a (non-owning) const char*
and size_t length. The memory pointed to be s1 + s2 is a temporary
std::string that is deleted by the end of the statement, giving you
a dangling pointer. (That errs() << sr; worked is an
accident).<br>
<pre class="moz-signature" cols="72">--
Joshua Cranmer
Thunderbird and DXR developer
Source code archæologist</pre>
</body>
</html>