Thread: seg fault

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

    seg fault

    Hey guys i did a backtrace using gdb and im getting a segfault when i call
    checkduplication function in my load function.

    i get the segfault here where the strcmp is i have no idea why? Am i
    trying to access something that isnt there im not sure?

    current = current->nextCategory;

    Also am i doing the validation correctly for each malloc?

    Code:
    /* checks for duplication of fields the catID field*/
    int checkDuplicationID(GJCType *menu, char* catID)
    {
        CategoryTypePtr current;
        current = menu->headCategory;
        
        
        while(current !=NULL)
        {
            if(strcmp(current->categoryID, catID) == ERRORCODE)
    	{
    	  printf("A duplicate record was found error:\n\n");
    	  return ERRORCODE;
    	}
            
            /* i get a seg fault on this next line*/
            current = current->nextCategory;
        }
        
        return 1;
    }

    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 ==NULL || 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 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, "|");
    	   
    	   /* checking whether it tokenizez*/
    	   if(catID == NULL)
    	   {
    	      printf("CatID missing\n");
    	   }
    	   
    	   /*checks for a duplication of the id field*/
    	   if(!checkDuplicationID(menu, catID))
    	   {
    	      return ERRORCODE;
    	   }
               /* gets the second field and tokenizez it*/
    	   catType = strtok(NULL, "|");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catType == NULL)
    	   {
    	      printf("CatType missing\n");
    	   }
    	   
    	   catName = strtok(NULL, "|");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catName == NULL)
    	   {
    	      printf("CatName missing\n");
    	   }
    	   catDescription = strtok(NULL, "\0");
    	   
    	    /* checking whether it tokenizez*/
    	   if(catDescription == NULL)
    	   {
    	      printf("CatDescription missing\n");
    	   }
    	   
    	   /* 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));
    	   
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    	   
    	   /* malloc the pointers*/
    	   newP->headCategory = malloc(sizeof(CategoryType));
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    	   
    	   /* malloc the pointers*/
    	   newP->headCategory->headItem = malloc(sizeof(ItemType));
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    	   
    	   /* malloc the pointers*/
    	   newP->headCategory->nextCategory = malloc(sizeof(CategoryType));
    	   
    	   /* check if it cannot allocate memory*/
    	   if(newP == NULL)
    	   {
    	      printf("Memory could not be allocated\n\n");
    	      return ERRORCODE;
    	   
    	   }
    	   
    	   /* malloc the pointers*/
    	   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;
    	   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;
    }

    /* my structs header file*/
    Code:
    #ifndef GJC_H
    #define GJC_H
    
    /* System-wide header files. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* System-wide constants. */
    #define ID_LEN 5
    #define MIN_NAME_LEN 1
    #define MAX_NAME_LEN 25
    #define MIN_DESC_LEN 1
    #define MAX_DESC_LEN 250
    #define NUM_PRICES 3
    #define HOT 'H'
    #define COLD 'C'
    #define ERRORCODE 1
    #define VALID 0
    #define LINE_LENGTH 300
    #define TOKEN_PRODUCT 4
    
    typedef struct category* CategoryTypePtr;
    typedef struct item* ItemTypePtr;
    
    /* Structure definitions. */
    typedef struct price
    {
       unsigned dollars;
       unsigned cents;
    } PriceType;
    
    typedef struct item
    {
       char itemID[ID_LEN + 1];
       char itemName[MAX_NAME_LEN + 1];
       PriceType prices[NUM_PRICES];
       char itemDescription[MAX_DESC_LEN];
       ItemTypePtr nextItem;
    } ItemType;
    
    typedef struct category
    {
       char categoryID[ID_LEN + 1];
       char categoryName[MAX_NAME_LEN + 1];
       char drinkType;      /* (H)ot or (C)old. */
       char categoryDescription[MAX_DESC_LEN];
       CategoryTypePtr nextCategory;
       ItemTypePtr headItem;
       unsigned numItems;
    } CategoryType;
    
    typedef struct gjc
    {
       CategoryTypePtr headCategory;
       unsigned numCategories;
    } GJCType;
    
    int commandLineArguments(int argc, char* argv[]);
    int countToken(FILE *fp, char* temp, int tokenPerLine);
    #endif

  2. #2
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    Where is the pointer "menu" created? All I see is it's being passed to functions and a comparison being made

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    inside my main

    Code:
    #include "gjc.h"
    #include "gjc_options.h"
    #include "gjc_utility.h"
    
    int main(int argc, char* argv[])
    {
       /* declaration of variables*/
       GJCType menu;
      
       int cmd;
       char *menuFile;
       char *submenuFile;
       int openData;
       
       /* to check how many command line aguments entered*/
       commandLineArguments(argc,argv);
       
       
       /* populating arguments with pointers to files*/
       menuFile = argv[1];
       submenuFile = argv[2];
       
       /*initialize variables and pointers*/
       /*systemInit(&menu);*/
      
       /* populate the data in the files into memory*/
       openData = loadData(&menu, menuFile, submenuFile);
       
       /* if it does not open release the memory*/
       if(!openData)
       {
           systemFree(&menu);
           return EXIT_SUCCESS;
       }
    
       return EXIT_SUCCESS;
    }
    
    int commandLineArguments(int argc, char *argv[])
    {
      
        
       /* command line argument*/
       
       /* checks to see if 3 command line arguments have been entered*/
       if(argc<3)
       {
         
         printf("Invalid: Enter 3 command line arguments (Error:)\n");
         exit(ERRORCODE);
       }
       else if(argc >3)
       {
         printf("Too many arguments where supplied\n");
         return ERRORCODE;
       }
       
       
      
       return EXIT_SUCCESS;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM