LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segmentation fault in program at run time (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-in-program-at-run-time-838536/)

tkmsr 10-16-2010 03:59 PM

segmentation fault in program at run time
 
I am making a program to do a breadth first search.
The code which I am posting here just makes a que of the nodes visited in a binary tree in inorder fashion,so this implementation is not yet complete.
While developing I got a segmentation fault which I was not able to understand why I am getting so I am posting since the tree of same program (without BFS) is working.
Code:



#include<stdio.h>
#include<stdlib.h>
struct node {
    struct node *left, *right;
    int data, color;
} *root;
//root is used to point to the root node of tree
int check = 0;
typedef struct node tree;
struct list {
    struct list *next;
    struct node *btree_node_ptr;    //btree_node_ptr stores the address of members of structure node
} *que_start;
typedef struct list que;
tree *create_node(int);
void add_tree(int value, tree *);
void travel_tree(tree *);
void enque(tree *);
void travel_que(void);
void deque(tree *);
que *que_add_node(tree *);
que *tree_que;
int main()
{
    int i, j,value;
    tree *nv;
    root = NULL;
    que_start = NULL;
    value = 0;
    while (0) {
        printf("enter value \n");
        scanf("%d", &value);
        if (value == 1)
            break;
        if (root == NULL)
            root = create_node(value);
        else if (root != NULL) {
            //      nv = create_node(value);
            add_tree(value, root);
        }
    }
    travel_tree(root);
    printf("\n travelling in que \n");
    travel_que();   
}

tree *create_node(int num)
{
    tree *temp;

    temp = (tree *) malloc(sizeof(tree));
    temp->data = num;
    temp->left = NULL;
    temp->right = NULL;
    temp->color=2;
    return temp;

}

void add_tree(int v1, tree * node)
{

    if (v1 <= node->data) {
        if (node->left != NULL) {
            add_tree(v1, node->left);
        } else {
            node->left = create_node(v1);
        }
        return;
    }
    if (v1 > node->data) {
        if (node->right != NULL) {
            add_tree(v1, node->right);
        } else {
            node->right = create_node(v1);
        }
        return;
    }

}

void travel_tree(tree *temp)
{
//      printf("\n inside travel_tree");

    if (temp->left != NULL)
        travel_tree(temp->left);
    printf(" \n %d ", temp->data);
    enque(temp);
    if (temp->right != NULL)
        travel_tree(temp->right);
    return;
}

que *que_add_node(tree * temp)
{
    que *b;
    b = (que *) malloc(sizeof(que));
    b->next = NULL;
    temp->color=1;
    b->btree_node_ptr = temp;
    return b;
}

void enque(tree *temp)
{
//to make a que of que
    tree_que=que_start;
    if (que_start == NULL) {
        que_start = que_add_node(temp);
        tree_que=que_start;
        return;
    }
    else
        {    while(tree_que->next!=NULL)
        {
        tree_que=tree_que->next;
        }
    tree_que->next=que_add_node(temp);
    }
    return;
}
void travel_que(void)
{
    tree_que=que_start;
    while (tree_que!=NULL)
        {
    printf("Data is %d color is %d\n",tree_que->btree_node_ptr->data,tree_que->btree_node_ptr->color);
    tree_que=tree_que->next;
    }
    return;
}
void deque (tree *temp)
{
 while (tree_que!=NULL)
    {
    tree_que=tree_que->next;

    }
    return;
}


paulsm4 10-16-2010 04:20 PM

"Use the source, Luke":

Assuming you're using GCC or G++:

1. (Re)build your code with "-g" (enable debugger)
2. Run your program in gdb
3. Type "where" to get a stack traceback when the problem occurs

PS:
Here's a good tutorial on using GDB:
http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html

tkmsr 10-16-2010 04:22 PM

The problem was that tree could not be travelled.
while(0)
any thing inside this statement was not executing.
I could not understand why.
I changed while(0) to while(1==1) and it worked,

rupertwh 10-16-2010 04:37 PM

Quote:

Originally Posted by tkmsr (Post 4129774)
Code:


int main()
{
    int i, j,value;
    tree *nv;
    root = NULL;
    que_start = NULL;
    value = 0;
    while (0) {
            ...
        }
    }
    travel_tree(root);
    printf("\n travelling in que \n");
    travel_que();   
}


Of course it segfaults...

paulsm4 10-16-2010 05:13 PM

Frankly, you've just given a very good reason why it would be a good idea to familiarize yourself with a debugger (if you haven't already).

ALSO:
1. As you know, this means "while (FALSE)":
Code:

  while (0)
2. But, for the same reason, "while (1 == 1)" is duplicate redundant ;):
Code:

  while (1 == 1) // Comparing any number with itself is always TRUE

  // Equivalent:
  // In C/C++, any non-zero number BY ITSELF also evaluates "true"
  while (1)

  // Equivalent:
  // This is an idiom Kernighan and Ritchie use
  for ( ;; )

3. This is also redundant (at least in this case):
Code:

  // Poor
  if (root == NULL)
    root = create_node(value);
  else if (root != NULL)
    add_tree(value, root);

  // Better
  if (root == NULL)
    root = create_node(value);
  else
    add_tree(value, root);

  // Best
  if (!root)
    root = create_node(value);
  else add_tree(value, root);

Small stuff: the compiler will often optimize away this kind of stuff. But the cleaner you write, the easier it is to read and maintain.

IMHO ...


All times are GMT -5. The time now is 05:38 PM.