segmentation fault

This is a discussion on segmentation fault within the C Programming forums, part of the General Programming Boards category; I get a segfault not sure from what could someone help me debug it please? Code: int loadData(GJCType* menu, char* ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    segmentation fault

    I get a segfault not sure from what could someone help me debug it please?


    Code:
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
       /*pointer for malloc*/
       GJCType *newP;
    
    
       /*declaration of variables*/
       int CountNoLines = 0;
       char temp[LINE_LENGTH];
       
       /* variables for tokenizer for menu file*/
       char *catID;
       char *catType;
       char *catName;
       char *catDescription;
      
       
       /* declare file pointers to files*/
       FILE *f1;
       FILE *f2;
         
       
       /* opening the menu and submenu for reading*/
       f1 = fopen(menuFile, "r");
       f2 = fopen(submenuFile, "r");
       
       /* check to see if the file exists*/
       if(f1 ==NU
    
    LL || f2 == NULL)
       {
          /* check whether both files have existed or typed in correctly*/
          if(f1 == NULL && f2 == NULL)
          {
          
             printf("Both files do not exist %s %s\n",menuFile,submenuFile);
          }
          /* check for each file existance*/
          else if (f1 == NULL)
          {
             printf("%s does not exist\n\n",menuFile);
          }
          /* check for each file existance*/
          else if(f2 == NULL)
          {
             printf("%s does not exist\n\n", submenuFile);
    	 
          } 
          printf("EXITING PROGRAM ERROR!\n\n");
          return ERRORCODE; /* cannot proceed*/
       }
       
       /* counts how many fields there are in the file*/
       while(fgets(temp, LINE_LENGTH, f1)!=NULL)
       {
           
           /*temp[strlen(temp)]= '\0';
           printf("%s", temp);*/
           if(!countToken(f1, temp, TOKEN_PRODUCT))
           {
              
    	  return ERRORCODE;
    	  
           }
           CountNoLines++;
       }
      
       printf("%d", CountNoLines);
       /* set f1 pointer to the start of the file for reading*/
       fseek(f1, 0, SEEK_SET);
       
       /* if CountNoLines is 0 do nothing*/
       if(CountNoLines == 0)
       {
          ;
       }
       
       /* if there is more than 0 lines tokenize fields
       check if there is duplication and check the string 
       length and if its too big its the wrong data format
       */
       
       if(CountNoLines > 0)
       {
           while(fgets(temp, LINE_LENGTH, f1) !=NULL)
           {
               /* stores the catID but also checks if there
    	   is a duplicate*/
    	    
               catID = strtok(temp, "|");
    	   
    	   /*checks for a duplication of the id field*/
    	   if(!checkDuplicationID(menu, catID))
    	   {
    	      return ERRORCODE;
    	   }
               /* gets the second field and tokenizez it*/
    	   catType = strtok(NULL, "|");
    	   catName = strtok(NULL, "|");
    	   catDescription = strtok(NULL, "\0");
    	   
    	   /* checks the string length of the field*/
    	   if((strlen(catID) >ID_LEN) ||
    	      (strlen(catType) >MAX_NAME_LEN)  ||
    	      (strlen(catDescription) >MAX_DESC_LEN))
    	   {
    	      printf("Wrong data format\n\n");
    	      return ERRORCODE;
    	      
    	   }
    	   
    	   /* malloc the pointers*/
    	   
    	   newP = malloc(sizeof (GJCType));
    	   newP->headCategory = malloc(sizeof(CategoryType));
    	   newP->headCategory->headItem = malloc(sizeof(ItemType));
    	   newP->headCategory->nextCategory = malloc(sizeof(CategoryType));
    	   newP->headCategory->headItem->nextItem =malloc(sizeof(ItemType));
    	   
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    	   
    	   /*copy variables into structure*/
    	   
    	   strcpy(newP->headCategory->categoryID, catID);
    	   
    	   newP->headCategory->drinkType = *catType;
    	   
    	   
    	   
    	   /*printf("%c", newP->headCategory->drinkType);*/
    	  /* strcpy(newP->headCategory->drinkType, catType);*/
    	  
    	   strcpy(newP->headCategory->categoryName, catName);
    	   strcpy(newP->headCategory->categoryDescription, catDescription);
    	   newP->headCategory->nextCategory = NULL;
          }
          
          
      }	   
          
       
       
       /* close both files after reading*/
       if(fclose(f1)!=0 || fclose(f2)!=0)
       {
          fprintf(stderr, "Error in closing files\n");
       }   
      
    
       return EXIT_SUCCESS;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Try compiling with the -g option. Then after the segfault you should get a core dump that gdb can use. After the segfault do gdb myprogramname core and gdb should tell you exactly what line it crashed on. From there you can get all sorts of useful information such as what variables had what values at the time of the crash.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    In an earlier post, someone told me to try valgrind to look for memory leaks.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Take itsme's suggestion and pinpoint the location of the segfault. That'll help get your answer quicker. A few notes:
    Code:
       if(CountNoLines == 0)
       {
          ;
       }
    I hope you know that that does nothing? At all?

    If only one file fails to open, the other does not get closed in your error checking.
    If strtok fails, and returns NULL, your program is doomed.
    If any of your memory allocations fail, your program is screwed.
    This:
    Code:
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    Is useless. By this point, you'll have since segfaulted since you attempt to set members of newP. Move this.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 07:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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