2014-11-23 1 views
-1

내 코드가 char * 줄을 char '로 분리합니다. cmds, 먼저 문자'| ' 다음 공백, \ n 등 샘플에서 I는 O/:Realoc : 줄을 나누는 동안 다음 크기가 유효하지 않습니다.

는 I : line = "ls -l/| unique | sort"

O : 그것은 1 개 이상의 단어 선 *cmds = realloc(*cmds, nlines+1); 도달 할 때마다 지금 cmds = {{"ls", "-l", "/", NULL}, {unique, NULL}, {sort, NULL}, NULL}

, 그것은 오류

생산 *** 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.

(`* CMDS = realloc을 : 어떤 도움

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

를 해결하기 위해 ... 나는 이미 몇 시간에 시간을 보냈다, 주시면 감사하겠습니다 * cmds, nlines + 1);'. nlines + 1 포인터 공간이 필요합니다. –

+0

또한,''cmds = '\ 0'. ** cmds는 char가 아닌 char에 대한 포인터입니다. http://c2.com/cgi/wiki?ThreeStarProgrammer –

+0

어떻게 해결할 수 있는지 알려주시겠습니까? 나는 그 순간을 배우는 시간과 방법을 모른다./ – user3885166

답변

0

샘플 당신은 여기 NLINES + 1 바이트를 할당하고

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; 
} 
관련 문제