LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2010, 03:59 PM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
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;
}
 
Old 10-16-2010, 04:20 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"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
 
Old 10-16-2010, 04:22 PM   #3
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
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,
 
Old 10-16-2010, 04:37 PM   #4
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by tkmsr View Post
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...
 
Old 10-16-2010, 05:13 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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 ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Segmentation Fault when im trying to start this program :/ Fronix Linux - Server 5 12-30-2008 12:34 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
C++ Program, Segmentation Fault Fireball7 Programming 6 12-07-2005 04:22 PM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 12:47 PM
"segmentation fault" when run a simple c program acer_peri Programming 11 05-28-2004 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration