Thread: help with segmentation fault, binary tree

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    help with segmentation fault, binary tree

    I have tried several solutions from other sites, but none of them worked. It always gives this "segmentation fault" message. please tell me what's wrong. thanks in advance!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tnode {
    	char *word;
    	int count;
    	struct tnode *left;
    	struct tnode *right;
    };
    
    struct tnode *addtree ( struct tnode *p, char *w );
    struct tnode *talloc ( void );
    int strcmp ( char *t, char *s );
    void cpword ( char *from, char *to );
    void treeprint ( struct tnode *p );
    char getword ( char *word );
    
    int main ( void )
    {
    	char word [20];
    	struct tnode *root;
    
    	printf ( "enter the text below:\n\n" );
    	while ( ( getword ( word ) ) != EOF )
    		if ( isalpha ( word ) )
    			addtree ( root, word );
    
    	printf ( "\n%-20s: %s\n\n", "word", "count" );
    	treeprint ( root );
    	return 0;
    }
    
    struct tnode *addtree ( struct tnode *p, char *w )
    {
    	int temp;
    
    	if ( p == NULL ) {
    		p = talloc ();	
    		cpword ( w, p->word );
    		p->left = NULL;
    		p->right = NULL;
    		p->count = 0;
    	}
    	else if ( ( temp = strcmp ( w, p->word ) ) == 0 )
    		++p->count;
    	else if ( temp < 0 )
    		p->left = addtree ( p->left, w );
    	else
    		p->right = addtree ( p->right, w );
    
    	return p;
    }
    
    struct tnode *talloc ( void )
    {
    	return ( ( struct tnode * ) malloc ( sizeof ( struct tnode ) ) );
    }
    
    int strcmp ( char *t, char *s )
    {
    	while ( *t && *s ) {
    		if ( *t == *s ) {
    			t++;
    			s++;
    		}
    		else
    			return ( *t - *s );
    	}
    	return 0;
    }
    
    void cpword ( char *from, char *to )
    {
    	while ( *from )
    		*( to++ ) = *( from++ );
    	*to = '\0';
    }
    
    void treeprint ( struct tnode *p )
    {
    	if ( p != NULL ) {
    		treeprint ( p->left );
    		printf ( "%-20s: %d\n", p->word, p->count );
    		treeprint ( p->right );
    	}
    }
    
    char getword ( char *word )
    {
    	char temp;
    	char *w = word;
    
    	while ( !isalnum ( temp = getchar () ) )
    		;
    	while ( isalnum ( temp ) ) {
    		*w++ = temp;
    		temp = getchar ();
    	}
    	*w = '\0';
    
    	return *word;
    }
    Last edited by Salem; 02-09-2011 at 10:34 AM. Reason: Removed the crappy font tags messing up the code tags

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Do you have your compiler's warnings turned on? This shouldn't go by unnoticed:
    Code:
    if ( isalpha ( word ) )
    isalpha() expects an int, not a char *.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    addtree() returns a root, which you're ignoring.

    Your root is also uninitialised.
    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.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Salem that’s actually good catch.

    @OP - Could tell us where exactly does the SEG FAULT occurs. For me tho, the problem really start from on how you read a string and there is no break point to it. And its just keep going.

    In addition could take the Salems advice on handling root pointer carefully. You will have initialised the pointer before you send them into the addtree function. As the pointer should pointer to random segment in the memory but not NULL. Which would eventually skip you if condition in the addtree function.

    And also the way you store the read char into the array word ( or 'w' in your case is very dangerous). Doing this
    Code:
    *w++
    Could lead into pointer moving beyond the boundary. Is there specific reason on not using function like fgets to read string of the user?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    This isn't the cause of your segfault, but it's worth noting that your strcmp doesn't actually work when one string is a substring of the other, e.g. "foo" and "foobar". Can I ask why you're using your own strcmp and cpword instead of strcmp and strcpy as provided by string.h?

    Also, getchar() returns an int, so you need to adjust getword's return type and variables accordingly so you can properly handle the EOF, or take Harish's suggestion to use fgets.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    thank u so much for the advices!

    @itsme86 yes it's turned on. i modified the code according to ur suggestion but it still returns the same error message.

    @Salem i had root initialized and added root = addtree ( root, word ). didn't work tho. but thanks for finding out my mistakes.

    @ssharish2005 it can print the "enter text below" message, but the error message shows up when i press enter. there was supposed to be a limit to how many letters can be stored in "word" (as in the getword function), but i figured i wouldn't need it to be that exact since i'm just learning how to do it, not writing a whole program or something. as for not using fgets... this is actually an example from a programming book whose author only used getchar. i wanted it to be more "original", so i didn't use functions that he didn't use.

    @anduril462 i knew that. i'm still learning so it may take time for me to write perfect function. i haven't figured out how to avoid that kind of imperfections yet. maybe referring to the standard library code could help. i need as many exercises as possible cuz i have to become a good programmer (i mean, like faster than others), so i treid to write every function i use.

    i'm kind of delirious right now so my words may not make sense to u. sorry. u can ask me to clarify if that does happen.

    so after all these modifications the error is still here. any more ideas?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    i forgot, @anduril462 i corrected the return type problem. thanks for pointing it out.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    @Harish sorry i didnt notice ur signature. "@ssharish2005 it can print the "enter text below" message, but the error message shows up when i press enter. there was supposed to be a limit to how many letters can be stored in "word" (as in the getword function), but i figured i wouldn't need it to be that exact since i'm just learning how to do it, not writing a whole program or something. as for not using fgets... this is actually an example from a programming book whose author only used getchar. i wanted it to be more "original", so i didn't use functions that he didn't use."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

Tags for this Thread