Я не могу решить "ошибка сегментации: 11" - PullRequest
0 голосов
/ 27 мая 2019

Я изучаю книгу, 2-е издание. Я написал код в книге, как показано ниже, и скомпилировал его, но он не работает. На моем терминале отображается сообщение «Ошибка сегментации: 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

Спасибо за ваше время.

Ответы [ 2 ]

2 голосов
/ 27 мая 2019

Есть следующие вещи неправильно

  1. Неверный if оператор в addtree. Изменить if ((p = NULL)) на if ((p == NULL))

  2. В функции talloc вы должны инициализировать left и right с помощью NULL

    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
    };
    
2 голосов
/ 27 мая 2019

ваша ошибка сегментации происходит от линии

if ((p = NULL))

должно быть

if (p == NULL)
...