Home > other >  Program received signal SIGSEGV, Segmentation fault when some other code added(c language)
Program received signal SIGSEGV, Segmentation fault when some other code added(c language)

Time:10-14

I am working with a program to create a avl tree. It works ok when I create a binary tree but when I run a function (I use it to find out the height of the curnode, it names getheight) the program down and it report 'Program received signal SIGSEGV, Segmentation fault'.
What I've tried: I searched for some similar questions but it seems those answers can't solve my question. Some answers describe what 'malloc' may need. But I didn't find where my program needs 'malloc' for my limited code level.
What I want to get: Obviously, I want to solve this question.
Some notition:You may ignore the annotated code.
The error occurs in "data < T->c"

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

typedef char ElemType;
typedef struct treenode {
  ElemType c;
  struct treenode* lchild;
  struct treenode* rchild;
} node;

int getheight(node* tree);

node* insert(ElemType data, node* T) {
  if (T == NULL) {
    T = (node*)malloc(sizeof(node));
    T->c = data;
    T->lchild = NULL;
    T->rchild = NULL;
    return T;
  } else {
    if (data < T->c) {
      T->lchild = insert(data, T->lchild);
      printf("left : %d %d\n", getheight(T->lchild), getheight(T->rchild));
    } else if (data > T->c) {
      T->rchild = insert(data, T->rchild);
      printf("right: %d %d\n", getheight(T->lchild), getheight(T->rchild));
    } else
      return T;
  }
}

int getheight(node* tree) {
  if (tree) {
    int lheight = getheight(tree->lchild);
    int rheight = getheight(tree->rchild);
    if (lheight > rheight)
      return lheight   1;
    else
      return rheight   1;
  } else {
    return 0;
  }
}

int main() {
  node* root;
  root = NULL;
  ElemType c = 0;
  while (1) {
    c = getchar();
    if (c == '\n') break;
    root = insert(c, root);
  }

  return 0;
}

CodePudding user response:

A SIGSEGV is a segfault, meaning your program tried to read from memory it isn't allowed to read from. Usually, this is because you dereferenced a NULL pointer, but it can be caused by some other things too. In your case, you aren't returning anything from a function. The problem is with an if-statement in your insert() function. You have this code to insert a node:

if (data < T->c) {
    T->lchild = insert(data, T->lchild);
    printf("left : %d %d\n", getheight(T->lchild), getheight(T->rchild));
} else if (data > T->c) {
    T->rchild = insert(data, T->rchild);
    printf("right: %d %d\n", getheight(T->lchild), getheight(T->rchild));
} else
      return T;

This code will only ever return T if data is equal to T->c. This is just a simple mistake/misconception on how if-else-statements work. The else block does not do what you want it to do. You want to return T at the end of the function, either way, so you should change the function to this:

node* insert(ElemType data, node* T) {
  if (T == NULL) {
    T = (node*)malloc(sizeof(node));
    T->c = data;
    T->lchild = NULL;
    T->rchild = NULL;
    return T;
  } else {
    if (data < T->c) {
      T->lchild = insert(data, T->lchild);
      printf("left : %d %d\n", getheight(T->lchild), getheight(T->rchild));
    } else if (data > T->c) {
      T->rchild = insert(data, T->rchild);
      printf("right: %d %d\n", getheight(T->lchild), getheight(T->rchild));
    }
    return T;
  }
}

This will still recursively run insert, but will return T.

  •  Tags:  
  • c
  • Related