【问题标题】:I cannot solve "segmentation fault: 11"我无法解决“分段错误:11”
【发布时间】:2019-05-27 01:26:28
【问题描述】:

我正在研究这本书,第 2 版。我在书中编写了如下代码并编译了它,但它不起作用。我的终端上显示消息“Segmentation fault: 11”。

上次我遇到了类似的问题,我通过在数组末尾添加 '\0' 来解决它。但这一次我不知道问题是从哪里开始的。

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

#define MAXWORD 100
#define BUFSIZE 100

char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
char *strdupH(char *);
void treeprint(struct tnode *);

struct key {
    char *word;
    int count;
};

struct key keytab[] = {
    "auto", 0,
    "break", 0,
    "case", 0,
    "char", 0,
    "const", 0,
    "continue", 0,
    "default", 0,
    "unsigned", 0,
    "void", 0,
    "volatile", 0,
    "while", 0
};

struct tnode {              /* the tree node */
    char *word;             /* points to the text */
    int count;              /* number of occurences */
    struct tnode *left;     /* left child */
    struct tnode *right;    /* right child */
};
// It is illegal for a structure to contain an instance of itself.
// struct tnode *left
// declares left to be a pointer to a tnode, not tnode itself



//
//  6_5SelfReferentialStructures.c
//
//
//  Created by Damian on 26/05/2019.
//


/* getword: get next word or character from input */
int getword(char *word, int lim)
{
    int c, getchH(void);
    void ungetchH(int);
    char *w = word;

    while (isspace(c = getchH()))
        ;
    if (c != EOF)
        *w++ = c;
    if (!isalpha(c)) {
        *w = '\0';
        return c;
    }
    for ( ; --lim > 0; w++)
        if (!isalnum(*w = getchH())) {
            ungetchH(*w);
            break;
        }
    *w = '\0';
    return word[0];
}

int getchH(void) /* get a (possibly pushed back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetchH(int c) /* push character back on input */
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}


/* word frequency count */
int main(int argc, char *argv[])
{
    struct tnode *root;
    char word[MAXWORD];

    root = NULL;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0])) {
            root = addtree(root, word);
        }
    treeprint(root);
    return 0;
}

/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
    int cond;
    if ((p = NULL)) {
        p = talloc();       /* make a new node */
        p->word = strdupH(w);
        p->count = 1;
        p->left = p->right = NULL;
    } else if ((cond = strcmp(w, p->word)) == 0)
        p->count++;         /* repeated word */
    else if (cond < 0)      /* less than into left subtree */
        p->left = addtree(p->left, w);
    else                    /* greater than into right substree */
        p->right = addtree(p->right, w);
    return p;
}

/* treeprint: in-order print of tree p */
void treeprint(struct tnode *p)
{
    if (!p) {
        treeprint(p->left);
        printf("%4d %s\n", p->count, p->word);
        treeprint(p->right);
    }

    return;
}

/* talloc: make a tnode */
struct tnode *talloc(void)
{
    return (struct tnode *) malloc(sizeof(struct tnode));
}

char *strdupH(char *s) /* make a duplicate of s */
{
    char *p;

    p = (char *) malloc(strlen(s) + 1); /* +1 for '\0' */
    if (p != NULL)
        strcpy(p, s);
    return p;
}

以上代码的结果是:

damian@James:~/t2/t2$ cc main.c -o main
damian@James:~/t2/t2$ ./main
Can you help me?
Segmentation fault: 11

感谢您的宝贵时间。

【问题讨论】:

  • “Segmentation fault” 听起来像是意大利语。
  • 您的代码输出Can you help me? 的方式很奇怪。我在代码本身的任何地方都看不到这一点。
  • addtree函数中将if (p = NULL)改为if(p == NULL)
  • 在调试器下运行程序
  • 你为什么要在p = NULL周围加上双括号

标签: c segmentation-fault kernighan-and-ritchie


【解决方案1】:

您的分段错误来自线路

if ((p = NULL))

应该是

if (p == NULL)

【讨论】:

    【解决方案2】:

    有以下几点不对

    1. addtree 中的 if 语句不正确。将if ((p = NULL)) 更改为if ((p == NULL))

    2. talloc函数中,你应该用NULL初始化leftright

      struct tnode *talloc(void)
      {
          struct tnode *newnode;
          newnode = malloc(sizeof(struct tnode));
          newnode -> left = NULL;
          newnode -> right = NULL;
          return (newnode); 
      }
      
    3. treeprint 函数应该是 if(p) 而不是 if(!p)

      void treeprint(struct tnode *p)
      {
          if (p) {
              treeprint(p->left);
              printf("%4d %s\n", p->count, p->word);
              treeprint(p->right);
          }
      
          return;
      }
      
    4. 您应该使用大括号初始化keytab 数组。

      struct key keytab[] = {
        {"auto", 0,},
        {"break", 0,},
        //etc
      };
      

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多