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