Segmentation Fault Please Help

This is a discussion on Segmentation Fault Please Help within the C Programming forums, part of the General Programming Boards category; I am coding for binary search tree . I am sure that this code is good as the compiler does ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Chennai, Tamil Nadu, India
    Posts
    1

    Segmentation Fault Please Help

    I am coding for binary search tree . I am sure that this code is good as the compiler does not throw any errors . But at run time i get the SEGMENTATION FAULT , CORE DUMPED error . Can anyone please help me out ?


    I am using CYGWIN IN WINDOWS XP.

    Code:
    #include<stdio.h>
    #include<malloc.h>
    
    typedef struct nodetype
    {
        int data;
        struct nodetype *left;
        struct nodetype *right;
    }node;
    
    void showtree(node *root);
    node * insert(node *root,int key);
        int main()
        {
            node *root=NULL;
            root=insert(root,5);
            root=insert(root,3);
            root=insert(root,4);
            root=insert(root,3);
            root=insert(root,2);
            root=insert(root,7);
            root=insert(root,6);
            root=insert(root,8);
    
    showtree(root);
    return 0;
        }
    
        node *insert(node *root,int key)
        {
            if(!root)
            {
                root=(node*)malloc(sizeof(node));
                root->data=key;
                root->left=NULL;
                root->right=NULL;
    
            }
            else if (key<root->data)
            {
                root->left=insert(root->left,key);
            }
            else if (key>root->data)
            {
                root->right=insert(root->right,key);
            }
            return root;
        }
        void showtree(node *root)
        {
            showtree(root->left);
            printf("%d/n",root->data);
            showtree(root->right);
        }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Segmentation fault means that you're in memory you are not suppost to. Take a debugger and step trough, you will see witch pointer is the bad guy. This way, you learn the most of you're own mistakes!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    When does showtree() know when to stop going down and down and down....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Code:
        void showtree(node *root)
        {
            showtree(root->left);
            printf("%d/n",root->data);
            showtree(root->right);
        }

    you are using recursion . and there is no terminating condition. hence there is a overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By phoneix_hallows in forum C Programming
    Replies: 8
    Last Post: 08-27-2009, 05:56 AM
  2. Segmentation fault?
    By Camambert in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2009, 11:21 AM
  3. Segmentation Fault
    By a o destruction in forum C Programming
    Replies: 2
    Last Post: 08-13-2009, 01:33 PM
  4. Segmentation fault
    By tameeyore in forum C Programming
    Replies: 4
    Last Post: 02-26-2005, 12:49 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21