1

スペースと引用符で単語をトークン化しようとしていますが、正しい出力の後に奇妙な malloc エラーが発生します。

この関数に次のようなものを受け入れてもらいたい:

hello world "SOme quote"

出力は次のようになります。

hello
world
"some quote"

または入力が次の場合:

hello world no quote 

出力は次のようになります。

hello
world
no
quote

しかし、今、私は得ています:

Hello
WOrld
"Hello World"
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0000000001760010 ***
a.out: malloc.c:2451: 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.
Aborted (core dumped)

出力は正しいようですが、後でめちゃくちゃになります

コードは次のとおりです。

int process_command(char command[80]){
char curr_char;
char *word;
int start_pos;
int i;
int len;
len = strlen(command);
for(i=0,start_pos=0;i<strlen(command);i++){
    curr_char = command[i];
    if (curr_char == ' '){
        if (command[i-1]==' ') {start_pos++;continue;}
        word = malloc(i-start_pos*(sizeof(char)));
        strncpy(word,command+start_pos,i-start_pos);
        printf("%s\n",word);
        free(word);

        start_pos =i+1;

    }
    else if (curr_char == '\"'){
        word= malloc(len-i*(sizeof(char)));
        strncpy(word,command+i,len);
        printf("%s\n",word);
        free(word);
        i=len+len;
    }

}

return 0;
}
int main(){
    char buffer[80] = "Hello   WOrld  \"Hello World\"";
    process_command(buffer);
    return 0;
}

問題は修正されました!ありがとう 更新されたコードは次のとおりです。

int process_command(char command[80]){
char curr_char;
char *word;
int start_pos;
int i;
int len;
int quote=0;
len = strlen(command);
for(i=0,start_pos=0;i<strlen(command);i++){
    curr_char = command[i];
    if (curr_char == ' '){      /*If there was a space found copy the stuff before the space*/
        if ( i>0 && command[i-1]==' ') {
            start_pos++;
            continue;
        }
        word = malloc(i-start_pos+1*(sizeof(char)));
        strncpy(word,command+start_pos,i-start_pos);
        word[i-start_pos+1]='\0';
        printf("%s\n",word);
        free(word);
        start_pos =i+1;

    }
    else if (curr_char == '\"'){        /*If a quote was found, copy the rest of the string and exit loop*/
        word= malloc(len-i+1*(sizeof(char)));
        strncpy(word,command+i,len-i);
        word[len-i+1]='\0';
        printf("%s\n",word);
        free(word);
        quote=1;
        break;
    }

}if (quote==0){                 /*If there was no quote in the string, get the last element*/
    word = malloc(len-start_pos+1*(sizeof (char)));
    strncpy(word,command+start_pos,len-start_pos);
    word[len-start_pos+1]='\0';
    printf("%s\n",word);
        free (word);
    }
    return 0;
}
int main(){
    char buffer[80] = "Hello   \"WOrld  test\"";
    process_command(buffer);
    return 0;
}

しかし、これがトークン化の効率的な方法であるかどうか疑問に思っていますか? これは、ユーザーが入力した指示を処理するためのものです。したがって、ユーザーが入力した場合

add 1 2 "SOme text"

クエリを 3 つの部分にトークン化して処理したいと考えています。そのために、トークン化してキューにプッシュし、後でアイテムを 1 つずつ取り出して処理できるようにします。

4

2 に答える 2

1

いくつかの問題があります。そのうちのいくつかは次のとおりです。

for(i=0,start_pos=0;i<strlen(command);i++){
    curr_char = command[i];
    if (curr_char == ' '){
        if (command[i-1]==' ') {start_pos++;continue;}      // accesses an invalid array offset when i == 0
        word = malloc(i-start_pos*(sizeof(char)));          // doesn't allocate space for null terminator
        strncpy(word,command+start_pos,i-start_pos);        // doesn't null terminate the string
        printf("%s\n",word);
        free(word);

        start_pos =i+1;

    }
    else if (curr_char == '\"'){
        word= malloc(len-i*(sizeof(char)));                 // doesn't allocate space for null terminator
        strncpy(word,command+i,len);                        // writes past the end of the allocated buffer
        printf("%s\n",word);
        free(word);
        i=len+len;                                          // not sure what the intent of this is?  use `break;`?
    }

}

一般に、strncpy()これはユーザーの期待どおりに動作するとは限らず、バグのあるコードに関与することが多いため、避ける必要があります。

また、引用の処理は非常に単純化されています。引用されたアイテムは常に文字列の最後のトークンであると想定しています。それは意図したものかもしれませんが、次のような一連のトークンでは機能しません。

"one and two" three
于 2012-07-04T00:50:55.273 に答える
1

これから行うstrncpyに十分なメモリを割り当てる必要があります。これらの 2 つの行はstrncpy、0 バイトも書き込むため、1 つずれています。

word = malloc(i-start_pos*(sizeof(char)));
strncpy(word,command+start_pos,i-start_pos);

これらの 2 行は意味がありません: len-i バイトを割り当ててから、それに len バイト (プラス 0 バイト) を書き込みます:

word = malloc(len-i*(sizeof(char)));
strncpy(word,command+i,len);
于 2012-07-04T00:35:34.630 に答える