strtok出现:Segmentation fault
时间:2010-10-13 来源:seans88
code:
编译正常
运行错误: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"; |
编译正常
运行错误: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"; |
正确了!!!
相关阅读 更多 +