【问题标题】:Segmentation fault error in BSTBST中的分段错误错误
【发布时间】:2014-02-07 01:48:45
【问题描述】:

当我在 main 中调用 find_word 函数时,我不断收到分段错误错误。 当添加一个词时,我想返回 1,当它找到那个词时,我希望它返回 1。 所以我也不确定我的插入方法是否正确。

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

struct node {
    char *word;
    struct node *left;
    struct node *right;
};
static struct node *root;


int init(void)
{
    struct node *new_node = malloc (sizeof(struct node));
    if(new_node==NULL){
        return 0;
    }

    else{
        root = new_node;
        new_node->left = NULL;
        new_node->right = NULL;
        return 1;
    }
}

static int insert(struct node *newnode, char *word)
{
    struct node *temp = NULL;
    if(!(newnode))
        {
            temp = (struct node *)malloc(sizeof(struct node));
            temp->left =NULL;
            temp->right = NULL;
            temp->word = word;
            newnode = temp;
            return 0;
        }

    if(word < (newnode)->word)
        {
            insert((newnode)->left, word);
        }
    else if(word > (newnode)->word)
        {
            insert((newnode)->right, word);
        }
    return 1;
}

int add_word(char *word)
{
    return insert(root,word);

}
static int find(char *word, struct node *newnode){
    if(newnode==NULL){
        return 0;
    }
    else if(strcmp(word,newnode->word)>0){
        find(word,newnode->left);
    }

    else if(strcmp(newnode->word,word)<0){
        find(word,newnode->right);
    }
    else{

        return 1;

    }
    return 0;
}


int find_word(char *word)
{
    return find(word,root);
}




int main(int argc,char *argv[])
{
    int k;
    char l[5];


    k = init();
    printf("init: %d\n",k);

    strcpy(l,"x");
    k = add_word(l);
    printf("add_word(%s): %d\n",l,k);

    strcpy(l,"x");
    k = find_word(l);
    printf("find_word(%s): %d\n",l,k);



    return 0;
}

【问题讨论】:

  • insert 应该使用strcmp 来比较wordnewnode-&gt;word,而不是&lt;&gt;
  • @Barmar 我更改了它,所以它现在是 strcmp(word,newnode-&gt;word)&gt;0strcmp(word,newnode-&gt;word)&lt;0insert 但我仍然收到分段错误。
  • 你之前不是发过类似的问题吗?好像被删了,因为现在找不到了。正如我在那里建议的那样,在调试器下运行您的代码,以便在发生错误时查看哪些变量无效。
  • 我做了,但我认为这会提供更多背景信息。我会尝试调试。
  • 您可能只是更新了原始问题而不是发布新问题。

标签: c segmentation-fault


【解决方案1】:

这样修复

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

struct node {
    char *word;
    struct node *left;
    struct node *right;
};

static struct node *root = NULL;

static int insert(struct node **newnode, char *word){
    struct node *temp = NULL;
    int cmp;

    if(!*newnode){
        temp = (struct node *)malloc(sizeof(struct node));
        temp->left =NULL;
        temp->right = NULL;
        temp->word = strdup(word);
        *newnode = temp;
        return 0;
    }

    if((cmp=strcmp(word, (*newnode)->word)) < 0)
        return insert(&(*newnode)->left, word);
    if(cmp > 0)
        return insert(&(*newnode)->right, word);
    return 1;
}

int add_word(char *word){
    return insert(&root, word);
}

static int find(char *word, struct node *newnode){
    int cmp;
    if(newnode==NULL)
        return 0;
    if((cmp=strcmp(word, newnode->word)) == 0)
        return 1;
    if(cmp < 0)
        return find(word, newnode->left);
    return find(word, newnode->right);
}

int find_word(char *word){
    return find(word, root);
}

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

    k = add_word(w="x");
    printf("add_word(%s): %d\n", w, k);

    k = find_word(w);
    printf("find_word(%s): %d\n", w, k);

    return 0;
}

【讨论】:

    【解决方案2】:

    如果newnode-&gt;word为NULL,则应在当前节点插入单词,处理空根节点。

    static int insert(struct node *newnode, char *word)
    {
        struct node *temp = NULL;
        if(!(newnode))
            {
                temp = (struct node *)malloc(sizeof(struct node));
                temp->left =NULL;
                temp->right = NULL;
                temp->word = malloc(strlen(word)+1);
                strcpy(temp->word, word);
                newnode = temp;
                return 0;
            }
    
        if (newnode->word == NULL) {
            newnode->word = malloc(strlen(word)+1);
            strcpy(newnode->word, word);
            return 1;
        }
    
        if(strcmp(word,(newnode)->word) < 0)
            {
                insert((newnode)->left, word);
            }
        else if(strcmp(word,(newnode)->word) > 0)
            {
                insert((newnode)->right, word);
            }
        return 1;
    }
    

    在您的find 函数中,您调用了两次strcmp。您交换了参数的顺序,但您也将&gt; 0 更改为&lt; 0。这些相互抵消,所以两者都在测试同样的东西。您需要更改其中之一,但不能同时更改两者。您还应该检查newnode-&gt;word == NULL

    static int find(char *word, struct node *newnode){
        if(newnode==NULL || newnode->word == NULL){
            return 0;
        }
        else if(strcmp(word,newnode->word)>0){
            find(word,newnode->left);
        }
    
        else if(strcmp(word,newnode->word)<0){
            find(word,newnode->right);
        }
        else{
    
            return 1;
    
        }
        return 0;
    }
    

    【讨论】:

    • 我已经更新了答案,展示了在将word 插入树时如何复制它。
    • 它仍然输出 b 仍然被发现,即使它没有被添加。我很困惑。
    • 修复了你的 find 函数。
    • 好的,所以它返回0,没有b,但在添加abc 时仍返回0?即使我现在明白我检查了两次同样的事情。
    • 对不起,我已经为你调试完了你的程序。如果你自己不知道如何做到这一点,你怎么会期望成为一名程序员?使用调试器,设置断点,单步执行程序,看看它在做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多