C - strtok(...) 上的意外分段错误 [英] C - Unexpected Segmentation Fault on strtok(...)

查看:35
本文介绍了C - strtok(...) 上的意外分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用库的 strtok(...) 并且它似乎工作正常,直到结束条件,在那里它导致分段错误和程序崩溃.API 声称 strtok(...) 将在找不到更多标记时输出 NULL,这意味着,我认为,您必须捕获此 NULL 才能终止使用 strtok() 运行的任何循环……).我需要做什么来捕获这个 NULL 以防止我的程序崩溃?我想象 NULL 被允许用作终止条件.

我准备了一个 SSCCE 供您观察这种行为.我需要 strtok(...) 才能为我正在编写的更大的软件工作,并且我得到了完全相同的分段行为.命令行的输出显示在此代码小插图下方(是的,我知道您使用 <...> 来封装库,但我很难让这篇文章显示代码库).我在 Windows 8 操作系统上使用 gcc 版本 4.5.3,下面展示了我想象的两种不同风格的人如何尝试在循环中捕获 NULL.

#include #include #include #include #include #include #include 主要的(){char* from = "12.34.56.78";字符 * ch = ".";char * token = strtok(from, ch);printf("%s\n",token);而(令牌!= NULL){token = strtok(NULL, ch);printf("%s\n", token);}printf("跳出循环!");而(strcmp(令牌,0)!= 0){printf("%s\n",token);token = strtok(NULL, ch);}}

<前>############ 输出: ############$ ./测试12345678分段错误(核心转储)

解决方案

您首先要检查 token 是否不等于 NULL(如果是,它会跳出 while循环).然后你正在比较 token,它是一个 NULL 和一个常量 NUMBER?这里: strcmp(token, 0)strcmp 需要 2 个字符串时,您提供一个数字.strcmp 将尝试在第 0 个地址(或 NULL)处获取字符串,从而导致分段错误.

while(strcmp(token, 0) != 0){token = strtok(NULL, ch);printf("%s\n",token);}

另外这段代码应该是这样的:

改变

 char * token = strtok(from, ch);printf("%s\n",token);而(令牌!= NULL){token = strtok(NULL, ch);printf("%s\n", token);}

 char * token = strtok(from, ch);printf("%s\n",token);而(令牌!= NULL){printf("%s\n", token);token = strtok(NULL, ch);}

I am using strtok(...) of the library and it appears to be working fine until the end condition, where it results in a segmentation fault and program crash. The API claims that strtok(...) will output a NULL when there are no more tokens to be found, which meant, I thought, that you had to catch this NULL in order to terminate any loops that you were running using strtok(...). What do I need to do to catch this NULL to prevent my program from crashing? I imagined the NULL was allowed for use as a terminating condition.

I have prepared a SSCCE for you to observe this behavior. I need strtok(...) to work for a much larger piece of software I am writing, and I am getting the exact same segmentation behavior. The output at the command line is shown below this code vignette (yes I know you use <...> to enclose libraries, but I was having difficulty getting this post to display the code libraries). I am using gcc version 4.5.3, on a Windows 8 OS, and below shows two different flavors of how I imagine one could try to catch the NULL in a loop.

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

main(){
  char* from = "12.34.56.78";
  char * ch = ".";
  char * token = strtok(from, ch);
  printf("%s\n",token);
  while(token != NULL){
    token = strtok(NULL, ch);
    printf("%s\n", token);
  }
  printf("Broke out of loop!");
  while(strcmp(token, 0) != 0){
    printf("%s\n",token);
    token = strtok(NULL, ch);
  }
}

############ OUTPUT: ############

$ ./test
12
34
56
78
Segmentation fault (core dumped)

解决方案

you are first checking if token is not equal to NULL(when it is, it breaks out of the while loop). Then you are comparing token, which is a NULL with a constant NUMBER? here: strcmp(token, 0) when strcmp expects 2 strings, you provide a number. strcmp will try to fetch a string at 0th address(or NULL) giving you a segmentation fault.

while(strcmp(token, 0) != 0){
    token = strtok(NULL, ch);
    printf("%s\n",token);
  }

Also this piece of code should be something like the following:

change

  char * token = strtok(from, ch);
  printf("%s\n",token);
  while(token != NULL){
    token = strtok(NULL, ch);
    printf("%s\n", token);
  }

to

  char * token = strtok(from, ch);
  printf("%s\n",token);
  while(token != NULL){
    printf("%s\n", token);
    token = strtok(NULL, ch);
  }

这篇关于C - strtok(...) 上的意外分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆