我该如何解决这个分段错误?

时间:2016-04-16 15:36:18

标签: c segmentation-fault

我一直想弄清楚我的代码有什么问题。所以在任何地方进出我的代码,我想我找到了罪魁祸首。正是这一块代码导致了它

   if (strcmp(input, "load 1") != 0 || strcmp(input, "load 2") != 0 || strcmp(input, "quit") != 0)
    {
        Player player;
        Position position;
        Direction direction;

        char* tok;
        char* firstInput=0;
        char* secondInput=0;
        char* thirdInput=0;
        char* fourthInput=0;
        int x;
        int y;
        char str[10];
        char str2[10];


        tok = strtok(input, " ");
        strcpy(firstInput, tok);            
        tok = strtok(NULL, ", ");
        strcpy(secondInput, tok);          
        tok = strtok(NULL, ", ");
        strcpy(thirdInput, tok);          
        tok = strtok(NULL, "");
        strcpy(fourthInput, tok);            

        strcpy(str, secondInput);
        x = atoi(str);
        strcpy(str2, thirdInput);
        y = atoi(str2);

        if(strcmp(firstInput, "init") == 0 )
        {
            if(x >= 0 && x <= 9)
            {
                if(y >= 0 && y <= 9)
                {
                    if (strcmp(fourthInput,"north") == 0 || (strcmp(fourthInput,"south")) || (strcmp(fourthInput,"west") == 0) || (strcmp(fourthInput,"east") == 0))
                    {
                        position.x = x;
                        position.y = y;

                        if(strcmp(fourthInput,"north") == 0)
                        {
                            direction = NORTH;
                        }
                        if(strcmp(fourthInput,"south") == 0)
                        {
                            direction = SOUTH;
                        }
                        if(strcmp(fourthInput,"west") == 0)
                        {
                            direction = WEST;
                        }
                        if(strcmp(fourthInput,"east") == 0)
                        {
                            direction = EAST;
                        }
                        initialisePlayer(&player, &position, direction);
                        placePlayer(board, position);
                        displayDirection(direction);
                    }
                }
            }
        }                      
    }

据我所知,分段错误意味着内存问题。我确保有足够的空间容纳一切。究竟发生了什么?

2 个答案:

答案 0 :(得分:1)

正如评论中所提到的,firstInput及其兄弟姐妹没有任何内存分配到strcpy到的地方。它们是NULL,这会导致分段错误。

您可以分配内存或将这些变量设为本地数组,但您并不需要这样做。 strtok返回指向input的指针。只要input未被覆盖,这些就有效。在将下一行读入input之前,您只使用这些指针来分析当前行,这样就可以了。

摆脱中间人tok并直接存储令牌的结果:

    char* firstInput = NULL;
    char* secondInput = NULL;

    firstInput = strtok(input, " ");
    if (firstInput) secondInput = strtok(NULL, ", ");

    // analyse input

处理指针时,请确保在访问之前测试NULL。无法保证用户输入恰好包含四个令牌(或任何令牌)。

答案 1 :(得分:0)

你可以从这里开始:

char* firstInput=0;
...
strcpy(firstInput, tok);

其中firstInput仍为NULL,并且没有为其分配内存,但您尝试将tok复制到其中。

使用malloc()分配内存,如下所示:

firstInput = malloc(sizeof(char) * (strlen(tok) + 1));
if (firstInput==NULL)
  exit (1);

这可以帮助您开始调试。 :)

相关问题