文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>strtok出现:Segmentation fault

strtok出现:Segmentation fault

时间:2010-10-13  来源:seans88

code:

31 char *buf1 = "aaa,bbb";
 32 char* token = strtok( buf1, ",");
 33 while( token != NULL )
 34 {
 35 printf( "%s ", token );
 36 token = strtok( NULL, ",-|");
 37 }


编译正常
运行错误:Segmentation fault
调试:
    在line32 char* token = strtok( buf1, ",");
Program received signal SIGSEGV, Segmentation fault.
0x00551df7 in strtok () from /lib/libc.so.6

原因分析:
man strtok

发现:BUGS
       Avoid using these functions.  If you do use them, note that:

              These functions modify their first argument.

              These functions cannot be used on constant strings.

              The identity of the delimiting character is lost.

              The  strtok()  function  uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.


因为 buf1 是一个常量,所以出错了;

代码修改:

31 char buf1[] = "aaa,bbb";
 32 char* token = strtok( buf1, ",");
 33 while( token != NULL )
 34 {



正确了!!!
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载