LINUX STL string malloc 指针乱了
有下面的一段代码,跟踪运行结果非常奇怪,
.................
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<1<<endl;}
p = (char *)malloc(temp.size()+40);
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<2<<endl;}
p1 = (char *)malloc(temp.size()+40);
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<3<<endl;}
p2 = (char *)malloc(temp.size()+40);
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<4<<endl;}
...............
这段的运行结果
headrule:1<=[a:1=2,not b:0=3]<=2 :- 4<=[a:1=5,not a:1=6]<=71
headrule:1<=[a:1=2,not b:0=3]<=2 :- 4<=[a:1=5,not a:1=6]<=72
headrule:1<=[a:1=2,not b:242044148160=3]<=2 :- 4<=[a:1=5,not a:1=6]<=73
~~~~~~~
这个地方的值莫名其妙地被修改了。
我是在Linux下使用了STL。
我把上面的代码换成
.............................
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<"1 "<<endl;}
temp.assign("and(le(");
ltoa(pwa->lowerbound, tmp);
temp.append(tmp);
temp.append(",");
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<"2 "<<endl;}
temp.append(str);
if (pwa->HeadRule != NULL)
{ cout<<"headrule:"; pwa->HeadRule->print(); cout<<"3 "<<endl;}
............................
结果与上面一样,输出:
headrule:1<=[a:1=2,not b:0=3]<=2 :- 4<=[a:1=5,not a:1=6]<=71
headrule:1<=[a:1=2,not b:0=3]<=2 :- 4<=[a:1=5,not a:1=6]<=72
headrule:1<=[a:1=2,not b:242044148160=3]<=2 :- 4<=[a:1=5,not a:1=6]<=73
~~~~~~~~~~~~~~也是莫名奇妙的被修改了(它是一个对象指针的成员)