gpt4 book ai didi

c - 解析文件中的字符串并创建二叉树的程序中出现段错误

转载 作者:行者123 更新时间:2023-11-30 15:31:19 24 4
gpt4 key购买 nike

我的程序在运行时收到段错误。我运行 gdb 编译器尝试找出它在哪里,但它向我显示了这条消息,而且我的代码中没有第 260 行哈哈,所以我不知道该怎么办。

Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse42 () at ../sysdeps/x86_64/multiarch/strcmp.S:260
260 movdqu (%rsi), %xmm2

我的程序所做的是读取文本文件并解析字符串,然后将它们存储到二叉树中。然后用户输入命令,我搜索树并查看该命令是否在树中列出。我将发布我的完整代码,以便大家都可以看到它,然后发布我读入的文件和示例输出,希望有人可以帮助我解决此段错误错误。预先非常感谢。

#include<stdlib.h>
#include<string.h>
#include<stdio.h>

#define COMMAND_NAME_LEN 50
#define MAX_SPLIT_SIZE 50
#define MAX_BUFF_SIZE 50

typedef struct Command_ {
char name[COMMAND_NAME_LEN];
int expected_param_count;
struct Command_ *left;
struct Command_ *right;
}Command;


typedef struct StringArray_ {
char **strings;
int size;
}StringArray;

StringArray* tokenizer (char *string, const char* delimiters);
void free_string_array(StringArray *sr);
void create_commands_tree(Command **commands, const char *file);
void insert_into_commands_tree(Command** node, char** data);
Command* get_command(Command *node, const char *command);
Command* create_command(char **data);
void destroy_commands_tree(Command* node);
void display_commands(Command *node);


int main (int argc, char *argv[]) {

if (argc < 2) {
printf("%s is missing commands.dat\n", argv[0]);
return 0;
}


Command* options = NULL;
create_commands_tree(&options,argv[1]);
int checking = 1;

char input_buffer[MAX_BUFF_SIZE];

do {
printf("Command: ");
fgets(input_buffer,MAX_BUFF_SIZE,stdin);
StringArray* parsed_input = tokenizer(input_buffer," \n");
Command* c = get_command(options,parsed_input->strings[0]);

if( c && parsed_input->size == c->expected_param_count) {
if (strcmp(c->name, "quit") == 0){
checking = 0;
}
printf("Valid command used\n");
}
else {
printf("Invalid command, please try again\n");
}
free_string_array(parsed_input);

}while (checking);

destroy_commands_tree(options);

}


void create_commands_tree(Command **commands, const char *file) {

FILE *input;
input = fopen(file, "r");
char strings[100];
StringArray *temp2;

while(fgets(strings,100,input)){

temp2 = tokenizer(strings, "\n");
insert_into_commands_tree(commands,temp2->strings);
}
}

void insert_into_commands_tree(Command** node, char** data) {

if(node == NULL){
*node = create_command(data);
}
else if( node != NULL){
if(strcmp(data[0],(*node)->name) < 0)
insert_into_commands_tree(&(*node)->left,data);
else if(strcmp(data[0], (*node)->name) > 0)
insert_into_commands_tree(&(*node)->right,data);
}


}

Command* create_command(char **data) {

Command* new_;
new_ = (Command*)malloc(sizeof(Command));
strncpy(new_->name, data[0], COMMAND_NAME_LEN);
new_->expected_param_count = atoi(data[1]);
new_->right = NULL;
new_->left = NULL;


return new_;

}

Command* get_command(Command *node, const char *command) {

Command *temp = node;
int compare;

if(temp){
compare = strcmp(node->name, command);
if(compare == 0){
return temp;
}
else if(compare < 0){
return (get_command(node->right, command));
}
else{
if(compare > 0){
return (get_command(node->left, command));
}}

}
return temp;
}

void destroy_commands_tree(Command* node) {

if( node == NULL){
return;
}

destroy_commands_tree(node->left);
destroy_commands_tree(node->right);
free(node);

}
void display_commands(Command *node) {


printf("\npickup <item>");
printf("\nhelp ");
printf("\nquit ");
printf("\nload <file>\n\n");

}
StringArray* tokenizer (char *string, const char* delimiters){

StringArray* sr = malloc(sizeof(StringArray));
sr->strings = malloc(MAX_SPLIT_SIZE * sizeof(char *));

size_t len;
char* hold;

(sr->strings)[0] = malloc(MAX_BUFF_SIZE * sizeof(char));
hold = strtok(string, delimiters);
int i;
for(i = 1; i < MAX_SPLIT_SIZE; i++){

hold = strtok(NULL, delimiters);
if(hold == NULL){
sr->size = i + 1;
break;
}
(sr->strings)[i] = malloc(MAX_BUFF_SIZE * sizeof(char));
strcpy((sr->strings)[i], hold);
}
return sr;
}

void free_string_array(StringArray *sr) {

int i;
for(i = 0; i < sr->size; ++i){
free(sr->strings[i]);
}
free(sr->strings);
free(sr);
}

这是给出的示例输出:

]$ ./a.out commands.dat 
Command: pickup 
Invalid command, please try again 
Command: pickup ball 
Valid command used 
Command: quit 1 
Invalid command, please try again 
Command: load 
Invalid command, please try again 
Command: load bak.sav 
Valid command used 
Command: help
Valid command used
Command: help 2 
Invalid command, please try again 
Command: quit 
Valid command used 

我们读入的文件如下:

pickup,2
help,1
quit,1
load,2

最佳答案

tokenizer 分配并返回一个指向 StringArray 的指针,但您永远不会释放它。您应该释放 create_commands_tree 中的每一个。

tokenizer丢弃来自strtok的第一个结果,这可能不是您想要的。您正在分配 sr->strings[0] 但从未将任何内容复制到其中。

create_commandinsert_into_commands_tree 应传递一个 StringArray,而不是 char **。您正在分配并初始化这个结构,您不妨使用它。否则这些函数将不知道有多少 token 被传递给它们。

关于c - 解析文件中的字符串并创建二叉树的程序中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com