[LLVMdev] create two Twine object

Eli Friedman eli.friedman at gmail.com
Sun Apr 18 03:11:08 PDT 2010


On Sat, Apr 17, 2010 at 9:55 PM, Yuanfang Chen <tabloid.adroit at gmail.com> wrote:
> I need to generate variables like
> status1, status2, status3, ......
> request1, request2, request3, ......
> this is my code, other unrelated detail are eliminated.
> static int varNum;
> static const char *getVarNum() {
> ++varNum;
> std::stringstream ss;
> ss << varNum;
> std::string *varname = new std::string(ss.str());
> return varname->c_str();
> }
> const char *VarNum = getVarNum();
> Twine *x1 = new Twine(StringRef("status"), VarNum);     //      1
> Twine *x2 = new Twine(StringRef("request"), VarNum);    //     2

This is dangerously wrong; the Twines end up with a dangling pointer
to an implicitly constructed StringRef.  Try using a SmallString
instead.

-Eli




More information about the llvm-dev mailing list