分段故障处理字符串的列C [英] Segmentation fault with array of strings C

查看:131
本文介绍了分段故障处理字符串的列C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了断​​线成空格分隔的标记,然后拷贝每个单独字符串转换成字符串数组的程序。

程序运行正常,直到它到达for循环,并在成功添加一个字符串到数组中,并打印出来,程序崩溃。我调试了一下,发现

  ARGS [I] =的malloc((strlen的(comm_str)+ 1)* sizeof的(炭));

收益段错误那么第二次执行循环。同时调用堆栈打印出以下内容:


  

地址:75F943F9,功能:strlen的(),文件:C:\\ WINDOWS \\ SysWow64资料\\ msvcrt.dll.`


我想正确的程序自己,但没有结果。我原先以为循环尝试访问外边界地区,但我想我已经正确malloc分配的一切。

 的#include<&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;诠释主(){    焦炭** ARGS = NULL;
    字符输入[] =CMDLINE -s -r 20 -t参数-p 20名;    INT NUM = 0;
    字符* comm_str = strtok的(输入,); / *令牌命令行* /    而(comm_str!= NULL){/ *计数的令牌数量* /
        NUM ++;
        的printf(%S%d个\\ N,comm_str,strlen的(comm_str));
        comm_str =的strtok(NULL,);
    }
    的printf(\\ n);    ARGS =的malloc(NUM * sizeof的(字符*)); / *分配内存的第一个字符串项* /
    如果(!参数){
        返回0;
    }    comm_str = strtok的(输入,); / *开始从开始tokening * /
    的for(int i = 0; I<民;我++){
        ARGS [I] =的malloc((strlen的(comm_str)+ 1)* sizeof的(炭)); / *分配内存的字符串* /
        如果(!ARGS [I]){
            对于(INT B = 0; B<我; b ++){
                免费(参数[B]);
            }
            免费(参数);
            返回0;
        }
        的strcpy(参数[I],comm_str);
        的printf(%S \\ n,ARGS [I]);
        comm_str =的strtok(NULL,);
    }    的printf(%d个\\ N,NUM);}


解决方案

strtok的是,如你所知,改变字符串。

计算的字的数量后,字符串将包含一个字。因此,接下来的 strtok的将返回NULL。

计算的参数的数目以非破坏性的方式,或者使字符串的副本。

I have made a program that breaks a string into tokens separated by space and then copies each separate string into an array of strings.

Program works fine until it reaches for-loop and after successfully adding the first string into the array and printing it, program crashes. I debugged it and found that

args[i] = malloc((strlen(comm_str) + 1) * sizeof(char));

returns SEGFAULT then performing loop for the second time. Also call stack prints out the following:

Address: 75F943F9, Function: strlen(), File: C:\Windows\syswow64\msvcrt.dll.`

I tried to correct program myself, but with no result. I thought at first that loop tries to access out of bound region, but I think I have malloc'd everything correctly.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(){

    char **args = NULL;
    char input[] = "cmdline -s 20 -r -t parameter -p 20 filename";

    int num = 0;
    char *comm_str = strtok(input, " ");                            /*Tokens command line*/

    while(comm_str != NULL){                                        /*Counts number of tokens*/
        num++;
        printf("%s %d\n", comm_str, strlen(comm_str));
        comm_str = strtok(NULL, " ");
    }
    printf("\n");

    args = malloc(num * sizeof(char*));                          /*Allocates memory for the first string entry*/
    if(!args){
        return 0;
    }

    comm_str = strtok(input, " ");                                  /*Starts tokening from beginning*/
    for(int i = 0; i < num; i++){
        args[i] = malloc((strlen(comm_str) + 1) * sizeof(char));    /*Allocates memory for strings*/
        if(!args[i]){
            for(int b = 0; b < i; b++){
                free(args[b]);
            }
            free(args);
            return 0;
        }
        strcpy(args[i], comm_str);
        printf("%s\n", args[i]);
        comm_str = strtok(NULL, " ");
    }

    printf("%d\n", num);

}

解决方案

strtok is, as you know, altering the string.

After calculating the number of words, the string will contain a single word. Hence the next strtok will return NULL.

Calculate the number of arguments in a non-destructive way, or make a copy of the string.

这篇关于分段故障处理字符串的列C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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