Chinaunix

标题: 高手们!解释解释这个std::string的append的问题! [打印本页]

作者: ftbk86    时间: 2013-09-28 02:59
标题: 高手们!解释解释这个std::string的append的问题!
代码如下:
   读取大文件,按照给定分割符对不同的数据项进行合并,合并后的值就是query的值
  
  std::vector<string> pParamresult = split(strParam,pattern1);
  std::vector<int> Indexresult;
  for(int i =0;i<pParamresult.size();i++)
  {
          Indexresult.push_back(atoi(pParamresult[i].c_str()));
  }
while (in && !in.eof())
  {
    queries.clear();
    while (getline(in, query))
    {
          std::vector<string> result=split(query,pattern);
          query.erase();          
          for(int i =0;i<Indexresult.size();i++)
          {
                query.append(result[Indexresult[i]]);
                query.append(pattern2);
          }
      queries[query] += 1;
    }
  }


但是程序运行段时间后,会出现如下问题:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b79de1 in std::string::append(std::string const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b79de1 in std::string::append(std::string const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000000000402877 in processInput(char const*, boost::function<void (std::string const&, long)> const& (file=0x61a140 "/data/Index_2013092500_5323_eth0.log", cb=...) at st.cpp:78
#2  0x0000000000402ed9 in sffle (nbuckets=10, argc=2, argv=0x7fffffffe550) at st.cpp:150
#3  0x0000000000403cbd in main (argc=7, argv=0x7fffffffe6c at st.cpp:371
此中append 值该如何操作是安全的!
作者: 井蛙夏虫    时间: 2013-09-28 12:07
回复 1# ftbk86
用gdb看看append时参数的值。


   
作者: myworkstation    时间: 2013-09-28 18:23
回复 1# ftbk86


    result[Indexresult]这个有问题的可能性最大,你能保证Indexresult表示result的维度一定是合法的吗?没有看到有进行合法性验证啊
作者: ftbk86    时间: 2013-09-28 19:09
if(result.size() < Indexresult[i])  加了这个以后,还是报错呢 !
query.append(result[Indexresult[i]]);还是这行报错
作者: myworkstation    时间: 2013-09-28 20:16
回复 4# ftbk86

加if(result.size() < Indexresult)判断是接着return的吗?
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2