【问题标题】:Segmentation Fault 11 error in C program where I would have to count occurrences of words enteredC 程序中的 Segmentation Fault 11 错误,我必须计算输入单词的出现次数
【发布时间】:2020-04-28 16:37:51
【问题描述】:

我的程序来自 K&R 的 C 编程语言书籍第 6.5 节,我们正在研究自引用结构。在这里,我们必须计算输入到终端的单词出现的次数。我们必须构建一个单词的二叉树并跟踪计数。我在 addtree 函数中收到分段错误 11 错误。我认为该函数中的 p 没有指向正确的内存。但我不知道如何解决它。下面是我的代码:

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

#define MAXWORD 100
#define BUFSIZE 100

struct tnode{
    char *word;
    int count;
    struct tnode *left; //left child. referential way of declaring
    struct tnode *right;
};

struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *);
int get_word(char *, int);
struct tnode *talloc(void);
char *my_strdup(char *);

int main(){
    struct tnode *root;
    char word[MAXWORD];
    while(get_word(word, MAXWORD) != EOF){
        if(isalpha(word[0])){
            printf("Still in main going in addtree function\n");
            root = addtree(root, word);
        }
    }
    treeprint(root);
    return 0;
}

struct tnode *addtree(struct tnode *p, char *w){
    int cond;
    printf("Inside addtree function.\n");

    if(p == NULL){ //a new word has arrived
        p = talloc();
        p->word = my_strdup(w);
        p->count = 1;
        p->left = p->right = NULL;
    }else if((cond = strcmp(p->word, w))==0)
        p->count++;
    else if(cond < 0)
        p->right = addtree(p->right, w);
    else
        p->left = addtree(p->left, w);
    return p;
}

void treeprint(struct tnode *p){
    if(p!=NULL){
        printf("Coming inside treeprint statement.\n");
        treeprint(p->left);
        printf("%4d    %s\n", p->count, p->word);
        treeprint(p->right);
    }
}

struct tnode *talloc(void){
    return (struct tnode *) malloc(sizeof(struct tnode));
}

char *my_strdup(char *w){
    char *p;
    p = (char *) malloc(strlen(w)+1);
    if(p!=NULL)
        strcpy(p, w);
    return p;
}

char buf[BUFSIZE]; //buffer for ungetch
int bufp = 0; //next free position in buf

int getch(void){
    return (bufp>0) ? buf[--bufp] : getchar();
}

void ungetch(int c){
    if(bufp >= BUFSIZE)
        printf("ungetch: too many characters.\n");
    else
        buf[bufp++] = c;
}

//get next word or character from input
int get_word(char *word, int lim){
    int c;
    int getch(void);
    void ungetch(int);
    char *w = word;
    while((isspace(c = getch())))
        ;
    if(c != EOF){
        *w++ = c;
    }
    printf("c: %d\n", c);
    printf("\n*w: %c", *w);
    if(!isalpha(c)){
        *w = '\0';
        return c;
    }
    for( ; --lim > 0; w++){
        if(!isalnum(*w = getch())){
            ungetch(*w);
            break;
        }
    }
    *w = '\0';
    printf("word[0]: %c\n", word[0]);
    return word[0];
}


【问题讨论】:

  • 你没有初始化变量 roort struct tnode *root;
  • 不保证在treeprint(root);,root已经初始化。
  • 函数中已经初始化为struct node *p。我不明白你试图暗示什么。请您显示编写我应该运行的正确代码行。
  • @RidwanChowdhury,ifstatement 的主体仅在满足条件时才执行,如果您说条件始终为真,那么没有意义,将来如果您想要答案,您必须标记发布者,否则他将不会收到通知。

标签: c unix segmentation-fault structure self-reference


【解决方案1】:

这里崩溃了:

}else if((cond = strcmp(p->word, w))==0)

在第一次运行时,因为 root->word 从未分配/初始化。

我强烈建议,在您学习 C 的过程中,您也应该学习使用 C 调试器。 Here is a great gdb tutorial。使用调试器,您几乎可以立即发现此类源问题。

如果您尝试运行此程序,gdb 的输出如下所示:

(gdb) run
Starting program: /home/nick/a.out 
hello darkness my old friend #<-- my keyboard input
c: 104

*w: ?word[0]: h
Still in main going in addtree function
Inside addtree function.

Program received signal SIGBUS, Bus error.
addtree (p=0x23792ac296e9f5c5, w=0x7f7fffff1680 "hello") at test.c:44
44      }else if((cond = strcmp(p->word, w))==0)
Current language:  auto; currently minimal

【讨论】:

  • 在写下你的答案之前,阅读问题的 cmets 有时会很有用。
  • 函数中已经初始化为struct node *p。我不明白你试图暗示什么。你能写出我应该运行的正确代码行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 2021-10-07
  • 2018-08-15
  • 1970-01-01
  • 2013-04-29
相关资源
最近更新 更多