Thread: Please help me to debug

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Red face Please help me to debug

    When i run my program in Unix, there are no warning and error,but when i run it, there are something called segmentation error, can any one help me to see what my problem is....thanks


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //#include"BinarySearchTree.h"
    int count=0,max=0,hight=0;

    typedef struct _bst{
    char key[30];
    struct _bst *left_child, *right_child;
    }bst;

    bst *search(bst *root,char name[30]);
    bst *insert(bst *root,char name[30]);
    bst *Delete(bst *root,char name[30]);
    bst *FindMin(bst *root);
    void print(bst *root);
    int GetHeight(bst *root);


    int main(int argc,char *argv[]){

    bst *node;
    char operation,name[30];

    while(1){
    scanf("%c",&operation);
    scanf("%s",name);
    switch (operation){
    case 'i':
    node=insert(node,name);
    break;
    case 's':
    if (search(node,name) == NULL){
    printf("True!");
    printf("%s can be find\n",name); }
    else
    printf("False\n");
    break;
    case 'h':
    count=max=0;
    printf("%d\n",GetHeight(node));
    break;
    case 't':
    hight=0;
    print(node);
    break;
    case 'd':
    Delete(node,name);
    break;
    case 'q':
    exit(1);
    default:
    printf("Operation character error, end the program\n");
    exit(1);
    }
    }

    return 0;
    }


    bst *insert(bst *root,char name[30]) {
    int side=strcmp(name,root->key);
    bst *ptr,*temp=search(root,name);
    if (temp || !(root)){
    ptr=(bst*)malloc(sizeof(*root));
    if (ptr== NULL){
    printf("The memory is full\n");
    exit(1);
    }
    strcpy(ptr->key,name);
    ptr->left_child=ptr->right_child = NULL;
    if (root)if (side<0) temp->left_child =ptr;
    else temp->right_child=ptr;
    else root = ptr;
    }
    return root;
    }

    bst *Delete(bst *root,char name[30]){
    int side=strcmp(name,root->key);
    bst *TmpCell;
    if(root== NULL )
    printf( "Element not found\n" );
    else
    if( side<0) /* Go left */
    root->left_child = Delete(root->left_child,name );
    else
    if( side>0) /* Go right */
    root->right_child = Delete( root->right_child ,name);
    else /* Found element to be deleted */
    if( root->left_child && root->right_child ) /* Two children*/
    {
    /* Replace with smallest in right subtree */
    TmpCell = FindMin( root->right_child );
    strcpy(root->key , TmpCell->key);
    root->right_child = Delete( root->right_child,root->key );
    }
    else /* One or zero children */{
    TmpCell = root;
    if( root->left_child == NULL ) /* Also handles 0 children*/
    root = root->right_child;
    else if( root->right_child == NULL )
    root = root->left_child;
    free( TmpCell );
    }
    return root;
    }

    bst *search(bst *root,char name[30]){
    int side=strcmp(name,root->key);
    if (!root) return NULL;
    if (side == 0) return NULL;
    if ((side<0) && (root->left_child))
    return search(root->left_child,name);
    if ((side>0) && (root->right_child))
    return search(root->right_child,name);
    return root;
    }


    bst *FindMin(bst *root){
    if( root == NULL )
    return NULL;
    else if( root->left_child == NULL )
    return root;
    else
    return FindMin( root->left_child );
    }

    int GetHeight(bst *root){
    //static int count =0,max=0; global variables
    if (root){
    count++;
    if (count > max)
    max=count;
    GetHeight(root->left_child);
    GetHeight(root->right_child);
    count--;
    }
    return max;
    }


    void print(bst *root) {
    int i;
    if (root){
    hight++;//hight is a static variable
    print(root->right_child);
    for(i=0;i<hight;i++)
    printf("\t");
    printf("%s",root->key);
    print(root->left_child);
    hight--;
    }
    }

  2. #2
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    use a debugger f e dbx
    dbx core
    then where
    the problem should be clear..

  4. #4
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    This is where I got the error on my debugger:
    Code:
    node=insert(node,name);
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    A crash will be caused on first call to insert(), passing pointer 'node' and using it before allocating memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM