[LLVMdev] create two Twine object

Yuanfang Chen tabloid.adroit at gmail.com
Sun Apr 18 09:45:03 PDT 2010


thank you guys. I see what the problem is now.

yuanfang

On Sun, Apr 18, 2010 at 12:16 PM, Daniel Dunbar <daniel at zuster.org> wrote:

> On Sun, Apr 18, 2010 at 4:36 AM, Eugene Toder <eltoder at gmail.com> wrote:
> > According to documentation Twines should be used only for temporary
> > values and not stored, so allocating the in heap sounds wrong.
>
> Yes, in general you should never be naming Twine directly, except in
> the case where you need to make a Twine for an integer. All other uses
> should be considered poor style, as they are dangerously subtle.
>
>  - Daniel
>
> > I think all you need here is
> >
> > static int varNum;
> >
> > ++varNum;
> > Instruction *sstatusInst = new AllocaInst(StatusTy, Twine("status") +
> > Twine(varNum), entry_inst);
> > Instruction *sreqInst = new AllocaInst(ReqTy, Twine("request") +
> > Twine(varNum), entry_inst);
> >
> >
> > btw, your getVarNum() leaks.
> >
> > Eugene
> >
> > On Sun, Apr 18, 2010 at 5:55 AM, 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
> >> Instruction *sstatusInst = new AllocaInst(StatusTy, *x1, entry_inst);
>  //
> >>  3
> >> Instruction *sreqInst = new AllocaInst(ReqTy, *x2, entry_inst);
>  //
> >>    4
> >> with only 1&3,  my code works well, I can have status1, status2,
> status3,
> >> ......
> >> with 1&2&3&4  exists at the same time,  it compiles without problem, but
> >> segmentation fault happens.
> >> Instruction *sreqInst = new AllocaInst(ReqTy, "request", entry_inst);
>  //
> >>   5
> >> with 1&2&3&5,  everything work, but that's not what I want.
> >> I really can't figure out what is broken. I create everything new on the
> >> heap.
> >> Each alloca inst with its own Twine object on the heap. Could anyone
> help me
> >> out?  Thanks a lot.
> >> yuanfang
> >> _______________________________________________
> >> LLVM Developers mailing list
> >> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> >>
> >>
> >
> > _______________________________________________
> > 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/20100418/faafc2b8/attachment.html>


More information about the llvm-dev mailing list