Realloc:分割线时无效的下一个尺寸

时间:2014-11-23 19:38:12

标签: c pointers segmentation-fault realloc

我的代码将char *行拆分为char *** cmds,首先按字符'|'然后通过空格,\ n等样本I / O:

我:line = "ls -l / | unique | sort"

O:cmds = {{"ls", "-l", "/", NULL}, {unique, NULL}, {sort, NULL}, NULL}

现在,每当它到达*cmds = realloc(*cmds, nlines+1);行超过1个单词时,就会产生错误

*** Error in ./a.out': realloc(): invalid next size: 0x000000000114c010 ***

a.out: malloc.c:2372: sysmalloc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.

任何帮助将不胜感激,我已经花了几个小时就已经......

void parse(char *line, char *** cmds)
{
printf("got line %s\n", line);
    size_t nlines = 0;
    *cmds =  NULL;

    while (*line != '\0') {
    nlines++;

        while (*line == ' ' || *line == '\t' || *line == '\n')
            *line++ = '\0'; 

        *cmds = realloc(*cmds, nlines+1);
        (*cmds)[nlines-1] = line; 
        (*cmds)[nlines] = NULL;    

        while (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\n')
            line++;
    }
    **cmds = '\0';  
}

void parsePipe(char *line, char ***cmds)
{
    char *cmd = strtok(line, "|");
    int linesFound = 0;

    while (cmd != NULL) 
    {
       printf("Printing word -> %s\n", cmd);
       linesFound++; 
       parse(cmd, cmds++);

       cmd = strtok(NULL, "|");
    }

    printf("This string contains %d lines separated with |\n",linesFound);
}

void  main(void)
{
    char  line[1024];          
    char  **cmds[64] = {0};

    while (1) {    
        printf("lsh -> ");   
        gets(line);     
        printf("\n");
        parsePipe(line, cmds);  
    }
}

1 个答案:

答案 0 :(得分:0)

要修复的示例

void parse(char *line, char *** cmds){
    char *cp, *token;
    size_t nlines = 0;
    *cmds =  NULL;

    token = strtok_r(line, " \t\n", &cp);
    while (token) {
        nlines++;
        *cmds = realloc(*cmds, sizeof(char*)*(nlines+1));
        (*cmds)[nlines-1] = token;
        (*cmds)[nlines] = NULL;
        token = strtok_r(NULL, " \t\n", &cp);
    }
}

void parsePipe(char *line, char ***cmds){
    char *cp;
    char *cmd = strtok_r(line, "|", &cp);
    int i=0, linesFound = 0;

    while (cmd != NULL){
        //printf("Printing word -> %s\n", cmd);
        linesFound++; 
        parse(cmd, cmds++);
        cmd = strtok_r(NULL, "|", &cp);
    }
    //printf("This string contains %d lines separated with |\n",linesFound);
    *cmds = NULL;
}

int  main(void){
    char  line[1024];
    char  **cmds[64] = {0};
    int i;

    while (1) { 
        printf("lsh -> ");
        scanf("%1023[^\n]%*c", line);
        if(*line == '.' && !line[1])
            break;
        parsePipe(line, cmds);
        //test print
        for(i=0; cmds[i]; ++i){
            int j=0;
            while(cmds[i][j]){
                printf("'%s' ", cmds[i][j++]);
            }
            printf("\n");
        }
    }
    //deallocation
    return 0;
}