내 코드가 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);
}
}
를 해결하기 위해 ... 나는 이미 몇 시간에 시간을 보냈다, 주시면 감사하겠습니다 * cmds, nlines + 1);'. nlines + 1 포인터 공간이 필요합니다. –
또한,''cmds = '\ 0'. ** cmds는 char가 아닌 char에 대한 포인터입니다. http://c2.com/cgi/wiki?ThreeStarProgrammer –
어떻게 해결할 수 있는지 알려주시겠습니까? 나는 그 순간을 배우는 시간과 방법을 모른다./ – user3885166