Thread: Unexplained Crashing

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    Unexplained Crashing

    Hello all,

    I'm currently looking at a very simple program which will read some information from a file, store this in a binary tree, then print it to the screen.
    Unfortunately, I'm having problems with it. The program is doing one of those annoying things where it isn't giving me any errors or warning, just crashing when I execute it.

    Heres the code for you to digest :

    Code:
      
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* construct tree */
    
    struct node
    {
       char *word;
       struct node *left,*right;
    };
    
    /* insert into tree*/
    
    struct node *inserttree(struct node *tree, char *s)
    {
       int i;
       
       if ( tree==NULL )
       {
          if ( (tree=(struct node *) malloc(sizeof (struct node)))!=NULL )
             if ( (tree->word=(char *) malloc(strlen(s)+1))==NULL )
             {
                free(tree);
                tree = NULL;
             }
             else
             {
                tree->left = NULL;
                tree->right = NULL;
                strcpy(tree->word, s);
             }
       }
       else if ( i = strcmp(tree->word, s) > 0 )
          tree->left = inserttree(tree->left, s);
    
       else if ( i < 0 )
          tree->right = inserttree(tree->right, s);
    
       else
          return tree;
    }
    
    /* print to console*/
    
    void printtree(struct node *tree, FILE *outf)
    {
         
       if ( tree != NULL )
       {
          printtree(tree->left, outf);
          printf("&#37;s\n", tree->word);
          printtree(tree->right, outf);
       }
    }
    
    /* destroy tree */
    
    void freetree(struct node *tree)
    {
       if ( tree!=NULL )
       {
          free(tree->word);
          freetree(tree->left);
          freetree(tree->right);
          free(tree);
       }
    }
    
    
    int main(void)
    {
    
       FILE *f,*outf;
    
       char s[100]; 
    
       struct node *tree=NULL;
       
    
       if ( (f=fopen("test.txt","r")) == NULL )
       {
          printf("Unable to open test.txt\n");
          system("PAUSE");
       }
    
       else
       {
          while ( fscanf(f,"%99s",s)!= EOF )
          {
             tree=inserttree(tree, s);
             printtree(tree, f);
          }
          fclose(f);
       }
    
       return 0;
    }
    Does anything jump out at anyone as a problem?
    Thanks for the help.
    Last edited by Swordsman; 05-10-2007 at 04:30 AM. Reason: Thanks to Dave for the beautifying :)

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Adding assumed headers, beautifying, and attempting to build:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* construct tree */
    
    struct node
    {
       char *word;
       struct node *left,*right;
    };
    
    /* insert into tree*/
    
    struct node *inserttree(struct node *tree, char *s)
    {
       int i;
    
       if ( tree==NULL )
       {
          if ( (tree=(struct node *) malloc(sizeof (struct node)))!=NULL )
             if ( (tree->word=(char *) malloc(strlen(s)+1))==NULL )
             {
                free(tree);
                tree = NULL;
             }
             else
             {
                tree->left = NULL;
                tree->right = NULL;
                strcpy(tree->word, s);
             }
       }
       else if ( (i = strcmp(tree->word,s))>0 )
          tree->left = inserttree(tree->left, s);
    
       else if ( i< 0 )
          tree->right = inserttree(tree->right, s);
    
       else
          return tree;
    }
    
    /* print to console*/
    
    void printtree(struct node *tree, FILE *outf)
    {
       if ( tree!=NULL )
       {
          printtree(tree->left,outf);
          printf("%s\n", outf/*tree->word*/);
          printtree(tree->right, outf);
       }
    }
    
    /* destroy tree */
    
    void freetree(struct node *tree)
    {
       if ( tree!=NULL )
       {
          free(tree->word);
          freetree(tree->left);
          freetree(tree->right);
          free(tree);
       }
    }
    
    
    int main(void)
    {
    
       FILE *f,*outf;
    
       char s[100]; 
    
       struct node *tree=NULL;
    
       if ( (f=fopen("words.txt","r")) == NULL )
       {
          printf("Unable to open word.txt\n");
          system("PAUSE");
       }
    
       else
       {
          while ( fscanf(f,"%100s",s)!= EOF )
          {
             tree=inserttree(tree, s);
             fclose(f);
             printtree(tree, outf);
             freetree(tree);
          }
          fclose(outf);
       }
    
       return 0;
    }
    main.c
    main.c: In function `inserttree':
    main.c:21: warning: suggest explicit braces to avoid ambiguous `else'
    main.c:42: warning: control reaches end of non-void function
    main.c: In function `printtree':
    main.c:51: warning: char format, FILE arg (arg 2)
    Linking...
    Build successful
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Hmm...wonder why I'm not getting them on DevC++ with the -W -wall -ansi -pedantic -O switches?

    Anyway, I'm suspicious of the printtree function. I know this sounds silly, but something doesn't look correct to me and my frazzled brain can't work out what. Can somebody help please?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you fix this yet?
    Code:
    printf("%s\n", outf/*tree->word*/);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Yeah sorry, I've got it as

    Code:
    printf("&#37;s\n", tree->word);

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why do you free the whole tree and close the file after every record?
    Do you know your fscanf format specifier is off by one too many?
    Have you tried stepping through the debugger?
    Would you care to make the input file available?
    Why C++ since this is C?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Quote Originally Posted by Dave_Sinkula View Post
    Why do you free the whole tree and close the file after every record?
    Do you know your fscanf format specifier is off by one too many?
    *Fixed*
    I had already taken out the free the tree/close file after I had posted, as it was one of those things that I had played with and not taken out. The fscanf I completely missed

    Why C++ since this is C?
    Errr....you've lost me, what in there is C++ ?

    Would you care to make the input file available?
    The input file is nothing but gibberish, just a few words in a text file, nothing special, although it if makes a difference I can post it.

    Have you tried stepping through the debugger?
    I have just done this and keep getting an "Access Violation:Segmentation Fault" when I call the printtree function. To be honest, I don't even know how to go about fixing that.

    Thanks for the help so far Dave, it is very much appreciated

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Swordsman View Post
    The input file is nothing but gibberish, just a few words in a text file, nothing special, although it if makes a difference I can post it.
    I would like to see what you see. Especially now that the code is very different from the last time you posted it.

    I'm just taking a stab in the dark here but this does look rather suspicious to me.

    It could be that on some execution paths of your inserttree function that i contains an undefined value that will almost certainly be greater than zero, or just... not what you're expecting i to be. This would cause code that normally would not be executed to execute anyway.

    > else if((i = strcmp( tree->word, s ) > 0)
    I tried to make some other point about how ugly this was, but I was wrong about that. Pretty much, this is the only path where i has a defined value.
    Last edited by whiteflags; 05-10-2007 at 01:48 AM. Reason: my own stupidity. it's late.

  9. #9
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    I edited the current code in to the top post.

    The input file is just literally a text file with the words "hello middle middle end" inside.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So have you run it in the debugger, and single stepped your way through it?

    What about removing the file stuff, and testing with a main of
    Code:
       struct node *tree=NULL;
       tree=inserttree(tree, "does");
       tree=inserttree(tree, "this");
       tree=inserttree(tree, "work");
       printtree(tree, stdout);
    If that crashes, then you have a nice simple test case.
    If not, then you can expand into reading from files.


    > printtree(tree, f);
    Why are you (trying) printing to the file you're reading from?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange windows STL crashing problem
    By kdmiller in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 02:25 PM
  2. std::map::find() crashing
    By noobcpp in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2008, 11:12 PM
  3. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  4. Replies: 5
    Last Post: 06-03-2005, 10:41 PM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM