LINUX STL string malloc 指针乱了

yswang168 2008-12-27 07:29:38
有下面的一段代码,跟踪运行结果非常奇怪,
.................
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
~~~~~~~~~~~~~~也是莫名奇妙的被修改了(它是一个对象指针的成员)

...全文
57 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ewuby 2009-02-09
  • 打赏
  • 举报
回复
这代码只见树木不见森林,太难分析,路过~(不过可以考虑下数据溢出的可能性)
gogoDNA 2009-02-09
  • 打赏
  • 举报
回复

pwa->HeadRule->print();
这句干了什么???

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧