LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 942|回复: 8

南橘北枳 —— Windows下生存,Linux下死亡,请教高手

[复制链接]
发表于 2003-4-11 15:08:36 | 显示全部楼层 |阅读模式
小弟写了一程序,在Windows下由vc,dev-cpp,编译通过,并顺利执行。

到了Linux7.2下,编译通过,但在执行时在执行到某一函数内时,出现Segmentation fault错误。



  1. void CPreprocess::ProcessSegment()
  2. {
  3.     int index(0);
  4.     cout << "进入ProcessSegment()" << endl;
  5.     map<string, string>::iterator posMap;
  6.     map<string, string>::iterator endMap = m_mapSegment.end();
  7.     vector<string>::iterator pos = m_vecKeyWordBuf.begin();
  8.     vector<string>::iterator end = m_vecKeyWordBuf.end();
  9.     for( ; pos != end; ++pos)
  10.     {
  11.         int len = pos->length();
  12.         for(int i=0; i<len; i+=2)
  13.         {
  14.             cout << "进入内部循环" << endl;
  15.             cout << "当前i " << i << endl;
  16.             string word = pos->substr(i, 2);
  17.             cout << "word : " << word << endl;
  18.             if((posMap = m_mapSegment.find(word)) != endMap)
  19.             {
  20.                 string buf = *pos;
  21.                 cout << "buf : " << buf << endl;
  22.                 buf.erase(i, 2);
  23.                 buf.insert(i, posMap->second);
  24.                 cout << buf << buf.size() << endl;
  25.                 m_vecKeyWordBuf.push_back(buf);
  26.                 m_vecAndClass.push_back(m_vecAndClass[index]);
  27.             }       
  28.         }
  29.         ++index;
  30.     }
  31.     cout << "退出ProcessSegment()" << endl;
  32. }
复制代码


这段代码的功能是将获得一个字符串中可拆分汉字拆分成两个汉字后的字符串(呵呵,比较饶口)

比如“好的”,经过这个函数处理后,就是“好白勺”。

m_mapSegment 是存储(的,白勺)的map<string, string>。
现在的问题是出在这行代码中:
现在的问题是当运行到某一处时,string word = pos->substr(i, 2);这行代码将不能工作,报错segmentation fault。
而这时候i,和i+2都在有效的范围内。


不过将
m_vecAndClass.push_back(m_vecAndClass[index]);

string word = pos->substr(i, 2);
任一行注释掉后可以正确运行~~

请高手指点,谢谢~~

我的gcc 版本是2.96. redhat linux 7.2
发表于 2003-4-12 01:25:21 | 显示全部楼层
这么看程序找错误太累了,要是我的话,就先用 gdb 看看。
 楼主| 发表于 2003-4-13 11:13:11 | 显示全部楼层
运行gdb,给出的信息是:
Program received signal SIGSEGV, Segmentation fault.
0x0805053f in basic_string<char,string_char_traits<char>, __default_alloc_temp)
      at/usr/include/g++-3/std/bastring.h:159
159 {return rep ()->len;}

用list得到如下代码:
155    public:
156      const charT* data() const
157        { return rep() ->date(); }
158      size_type length() const
159        { return rep()->len;}
160      size_type size() const
161        { return rep() ->len;}
发表于 2003-4-13 19:29:36 | 显示全部楼层
我遇到的情况恰恰相反。
linux下编译没有错误,可以windows下VC就出现了严重错误,说iostream的内部错误,让我联系微软。
怎么办?
 楼主| 发表于 2003-4-14 10:23:11 | 显示全部楼层
我这里在linux下编译也没有错误的,就是运行的时候有错误~~
发表于 2003-4-15 00:06:17 | 显示全部楼层
也许是你的程序有问题,很想帮忙,不过 debug 这种事儿,不好帮上手。。。
 楼主| 发表于 2003-4-16 09:18:50 | 显示全部楼层
呵呵,谢谢~~
问题已经解决了,是pos在push_back()后失效的bug。
不过仍然谢谢大家~~
发表于 2003-4-16 09:36:15 | 显示全部楼层
不知道你这个到底是怎么回事儿,我有时候 C++ 程序里面的 bug 很古怪,要认真想想才能明白。上次就是一个拷贝 constructor 没弄好,结果就 crash 了。
 楼主| 发表于 2003-4-17 08:54:38 | 显示全部楼层
呵呵,我这个就是在push_back后迭代器失效的问题~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表