Hi, I tried to get the name of function+loop, so I write a small function as following: StringRef getStringofFL(const Function * F, const Loop * L){ StringRef sr; std::string s1 = F->getName(); std::string s2 = L->getHeader()->getName(); sr = s1+s2; return sr; } However, two questions came: 1, if I output sr like: errs() << sr; it's ok (for instance: mainfor.cond ), but when I called this function and got the StringRef: StringRef s= getStringofFL(F, L); errs() << s; the result is: m\275[\377\177\000\0002\354\333\004\001, why? 2, I used the StringMap, and the StringRef got from the function getStringofFL as a key. Then I stored something into this StringMap, but I couldn't get it with the same function and loop. why? Thank you! Hanbing -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140422/4df01ca5/attachment.html>
On 4/22/2014 11:55 AM, Hanbing Li wrote:> Hi, > > I tried to get the name of function+loop, so I write a small function > as following: > > StringRef getStringofFL(const Function * F, const Loop * L){ > StringRef sr; > std::string s1 = F->getName(); > std::string s2 = L->getHeader()->getName(); > sr = s1+s2; > return sr; > } > > > However, two questions came: > > 1, if I output sr like: errs() << sr; it's ok (for instance: > mainfor.cond), but when I called this function and got the > StringRef: StringRef s= getStringofFL(F, L); errs() << s; the result > is: m\275[\377\177\000\0002\354\333\004\001,why? >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). -- Joshua Cranmer Thunderbird and DXR developer Source code archæologist -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140422/fe837ee7/attachment.html>
On 4/22/14, 9:55 AM, Hanbing Li wrote:> Hi, > > I tried to get the name of function+loop, so I write a small function as following: > > StringRef getStringofFL(const Function * F, const Loop * L){ > StringRef sr; > std::string s1 = F->getName(); > std::string s2 = L->getHeader()->getName(); > sr = s1+s2;To answer your first question, the std::string created by operator+ here gets destroyed when it goes out of scope. The StringRef holds on to a pointer to the buffer that that string used to own, making this a classic use-after-free bug.> return sr; > } > > > However, two questions came: > > 1, if I output sr like: errs() << sr; it's ok (for instance: mainfor.cond), but > when I called this function and got the StringRef: StringRef s= getStringofFL(F, > L); errs() << s; the result is: m\275[\377\177\000\0002\354\333\004\001,why? > > 2, I used the StringMap, and the StringRef got from the function getStringofFL > as a key. Then I stored something into this StringMap, but I couldn't get it > with the same function and loop. why? > > > > Thank you! > > > Hanbing > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- Jon Roelofs jonathan at codesourcery.com CodeSourcery / Mentor Embedded
Thank you all, It really works. Hanbing ----- Mail original -----> De: "Hanbing Li" <hanbing.li at inria.fr> > À: llvmdev at cs.uiuc.edu > Envoyé: Mardi 22 Avril 2014 18:55:04 > Objet: [LLVMdev] Little question about Stringref> Hi,> I tried to get the name of function+loop, so I write a small function as > following:> StringRef getStringofFL(const Function * F, const Loop * L){ > StringRef sr; > std::string s1 = F->getName(); > std::string s2 = L->getHeader()->getName(); > sr = s1+s2; > return sr; > }> However, two questions came:> 1, if I output sr like: errs() << sr; it's ok (for instance: mainfor.cond ), > but when I called this function and got the StringRef: StringRef s> getStringofFL(F, L); errs() << s; the result is: > m\275[\377\177\000\0002\354\333\004\001, why?> 2, I used the StringMap, and the StringRef got from the function > getStringofFL as a key. Then I stored something into this StringMap, but I > couldn't get it with the same function and loop. why?> Thank you!> Hanbing> _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140423/67030ffa/attachment.html>