【发布时间】:2021-06-03 15:11:44
【问题描述】:
我正在尝试使用标头 tokenizer.h 文件访问 tokenizer.c 中的方法,但是当我向安慰。 我试过重新排列指针和内存分配,我也得到了同学的帮助。我仍然收到错误消息。
#include <stdio.h>
#include <stdlib.h>
#include "tokenizer.h"
#include "history.h"
int main(){
int noexit = 1;
while(noexit){
char input[100];
printf("> ");
fgets(input, 100, stdin);
if(*input == '0'){
noexit = 0;
}
else{
char** tokens = tokenize(input);
print_tokens(tokens);
free_tokens(tokens);
}
}
//past output
/*printf(space_char(' '));*/
}
这是我的主要方法,在文件 uimain.c 中。这还没有完成,但只有当我删除了
char** tokens = tokenize(input);
print_tokens(tokens);
free_tokens(tokens);
阻止。
#include <stdio.h>
#include <stdlib.h>
#include "tokenizer.h"
int space_char(char c){
if(c == '\t' || c == ' '){
return 1;
}
return 0;
}
int non_space_char(char c){
if(c != '\t' || c != ' '){
return 1;
}
return 0;
}
char *word_start(char *str){
int i = 0;
while(space_char(str[i]) == 1){
i++;
}
return &str[i];
}
char *word_terminator(char *word){
word = word_start(word);
int i = 0;
while(non_space_char(word[i]) == 1){
i = i+1;
}
return &word[i];
}
int count_words(char *str){
int count = 0;
int i = 0;
while(str[i] != '\0') {
if(space_char(str[i]) && non_space_char(str[i + 1]))
count++;
i++;
}
count++;
return count;
}
char *copy_str(char *inStr, short len){
int i = 0;
//MALLOC FOR NEW STR :D
char *outStr = malloc((len+1) *sizeof(char));
while(i<=len){
outStr[i] = inStr[i];
i= i+1;
}
return outStr;
}
char **tokenize(char *str){
int i = 0;
printf("%s","int i");
int len;
printf("%s","len");
int all = count_words(str);
printf("%s","all");
char **tokens = malloc((all+1) * sizeof(char*));
printf("%s","tokens");
char *pointer = str;
printf("%s","pointer");
while(i < all+1){
pointer = word_start(pointer);
printf("%s","word_start");
len = (word_terminator(pointer) - word_start(pointer));
printf("%s","len");
tokens[i] = copy_str(pointer, len);
pointer = word_terminator(pointer);
i = i + 1;
}
tokens[i] = 0;
return tokens;
}
void print_tokens(char **tokens){
int i = 0;
while(tokens[i] != NULL){
printf("%s\n", tokens[i]);
i = i + 1;
}
}
void free_tokens(char **tokens){
int i = 0;
//can't pass len as param :D
while(tokens[i] != 0){
free(tokens[i]);
i = i + 1;
}
free(tokens);
}
这是 tokenizer.c
#ifndef _TOKENIZER_
#define _TOKENIZER_
/* Return true (non-zero) if c is a whitespace characer
('\t' or ' ').
Zero terminators are not printable (therefore false) */
int space_char(char c);
/* Return true (non-zero) if c is a non-whitespace
character (not tab or space).
Zero terminators are not printable (therefore false) */
int non_space_char(char c);
/* Returns a pointer to the first character of the next
space-separated word in zero-terminated str. Return a zero pointer if
str does not contain any words. */
char word_start(char *str);
/* Returns a pointer terminator char following *word */
char *word_terminator(char *word);
/* Counts the number of words in the string argument. */
int count_words(char *str);
/* Returns a fresly allocated new zero-terminated string
containing <len> chars from <inStr> */
char *copy_str(char *inStr, short len);
/* Returns a freshly allocated zero-terminated vector of freshly allocated
space-separated tokens from zero-terminated str.
For example, tokenize("hello world string") would result in:
tokens[0] = "hello"
tokens[1] = "world"
tokens[2] = "string"
tokens[3] = 0
*/
char **tokenize(char* str);
/* Prints all tokens. */
void print_tokens(char **tokens);
/* Frees all tokens and the vector containing themx. */
void free_tokens(char **tokens);
#endif
这是 tokenizer.h,以防万一
【问题讨论】:
-
使用调试器。它会立即告诉您触发 seg 错误的确切代码行。这是您应该做的最低限度的调试,并且应该在问题中发布。
-
-fsanitize=address(gcc & 我认为 clang) 非常适合调试这些问题。 -
好吧,一方面,
char word_start(char *str);肯定不符合其评论描述。该函数应返回char*而不是char -
关于:
char *outStr = malloc((len+1) *sizeof(char));1) 表达式:sizeof(char)在 C 标准中定义为 1。将任何内容乘以 1 没有任何效果,只会使代码混乱。建议删除该表达式。 2) 函数:malloc()需要size_t参数,而不是short参数。 3) 函数:malloc()可能会失败,因此应始终检查 (!=NULL) 返回值以确保操作成功。如果不成功(==NULL)则调用perror( "malloc failed" );清理并退出程序 -
关于如下语句:
printf("%s","word_start");这将位于stdout流缓冲区中,直到执行:program exits、buffer overflow或an input operation。或fflush()被调用或 '\n' 被输出。 IE。这不会以“及时”的方式显示在终端上。建议:printf("%s\n","word_start");
标签: c memory segmentation-fault coredump