Thread: GCC Segmentation fault error.

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

    GCC Segmentation fault error.

    all,

    I have the following code written for an assignment and it works great in DevC++. However, when using

    gcc -Wall -ansi -o pa6 animal.c

    I get a segmentation fault when trying to run the program. Can someone point me in the right direction?

    I apologize in advance for the horrible formatting.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    #include <string.h>
    
    #ifndef EleType
    #define EleType char
    #endif
    
    struct node {
       char * value;
       struct node * left;
       struct node * right;
    };
    
    struct node * newAnimalTree()
    {
           struct node * animalTree = (struct node *)malloc(sizeof(struct node));
           assert (animalTree !=0);
           animalTree->value= "Cat\n";
           animalTree->left = 0;
           animalTree->right = 0;
           return animalTree;
               
    };
    
    struct node * newLeftNode(struct node * current, EleType * newValue) 
    {
    
        struct node *node=(struct node *)malloc(sizeof(struct node));
        current->left=node;
        node->value = newValue;
        node->left = 0;
        node->right = 0;
        return current;
    } ;/*end newLeftNode*/
    
    struct node * newRightNode(struct node * current, EleType * newValue) 
    {
        
        struct node *node=(struct node *)malloc(sizeof(struct node));
        current->right=node;
        node->value = newValue;
        node->left = 0;
        node->right = 0;
        return current;
    } ;/*end newRightNode*/
    
    
    char * newStr (char * charBuffer)
    {
       char * p = (char *) malloc(1 + strlen(charBuffer));
       strcpy (p, charBuffer);
       return p;
    };/*end newStr*/
    
    struct node *readATree (FILE * f)
    {
      char c, buffer[100];
      struct node * newTree = (struct node *) malloc(sizeof(struct node));
      c = fgetc(f);
       fgets(buffer, 100, f);
      if (c == 'A'){
         newTree->value = newStr(buffer);
         newTree->left=0;
         newTree->right=0;
         }
         else
          {
         newTree->value = newStr(buffer);
         newTree->left = readATree(f);
         newTree->right = readATree(f);
         }
              
          return newTree;
         
    };
    void writeATree (struct node * tree, FILE *f)
    {
         if(tree != 0)
         {
                  if(tree->left !=0)
                  fputc('Q', f);
                  else
                  fputc('A', f);
                 
          fputs(tree->value, f);
          writeATree(tree->left, f);
          writeATree(tree->right, f);
         }/*end if*/
    
       
    }; /*end writeATree*/
    
    
    
    int main() { 
    char buffer[100]; 
    FILE * f;
    struct node * current;
    struct node * animalTree;     
    char *animal;
    char *question;
    printf("Think of an animal\n");
    f = fopen("animal.txt", "r");
    
    if(f !=0)
    {
    animalTree = readATree(f);
    }
    else
    {
    animalTree=newAnimalTree();
    }
    fclose(f);
    
    current= animalTree;
    while(current != 0)
    {
     if(current->left !=0)
     {
      printf("%s", current->value);
      fgets(buffer, 100, stdin);
       
          if(buffer[0] == 'Y' || buffer[0] == 'y')
          {
          current = current->left;
          }/*end inner if*/
          else
          {
          current = current->right;
          }/*end else*/
          
    	}/*end outer if*/
    	 
    else
    	{
      printf("Im going to guess %s", current->value);
      fgets(buffer, 100, stdin);
      
          if(buffer[0] == 'Y' || buffer[0] == 'y')
          {
                printf("I am the winner!\n");
                getchar(); 
                return 0;
          }/*end if*/
      
          else 
          {
              printf("Im stumped. What animal was it?\n");
            	fgets(buffer, 100, stdin);
            	animal = newStr(buffer);
            	    	
            	printf("Give a yes/no question that that is true of %s but not true of %s", current->value, animal);
            	fgets(buffer, 100, stdin);
            	question = newStr(buffer);
               
              /*create new nodes for animals*/  
              newLeftNode(current, current->value); 
           	  newRightNode(current, animal); 
              
            	current->value = question;
           	  /*write to file */
             f= fopen("animal.txt", "w");
             writeATree(animalTree, f);
             return 0;   
        }/*end else*/
    
    
    }/*end outer else*/
    
    }/*end while*/
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This code is a problem:
    Code:
    f = fopen("animal.txt", "r");
    
    if(f !=0)
    {
    animalTree = readATree(f);
    }
    else
    {
    animalTree=newAnimalTree();
    }
    fclose(f);
    What happens when f is NULL? Well, you try to do fclose(f), which is bad, because you cannot pass a null pointer to fclose().

    When you get a segfault, your first response should be to run your program through a debugger. Valgrind, if it's available for your platform, is great. Gdb (a different style of debugger) is also good.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Thank you for the help, that fixed the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM