still segmentation fault when printing an array of structs

This is a discussion on still segmentation fault when printing an array of structs within the C Programming forums, part of the General Programming Boards category; hi all, i posted this question earlier and someone responded with a helpful solution. It didnt totally fix the problem ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Question still segmentation fault when printing an array of structs

    hi all,

    i posted this question earlier and someone responded with a helpful solution. It didnt totally fix the problem i was having which is a segmentation fault and according to gdb its located on line 213 and on that line is the following code:
    tempStory = *tempPtr;
    (please note that this is in the function named printHeadlines() which is the last function in the code below)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <regex.h>
    
    #define MAXLINE 255
    
    struct headline {
    	
    	char *title;
    	char *link;
    	char *description;
    	
    };
    typedef struct headline Headline;  
    
    void readInFile(void);
    void printHeadlines(int *, Headline *[]);
    int match(const char *, char *, Headline *[], Headline *, int * );
    char getHeadlines(int, int, const char *, Headline *[], Headline *, int * );
    void storeHeadlines(char *, Headline * , Headline *[], int * );
    
    //made global so i dont have to keep passing it through each method
    char searchType[]="description";
    
    int main(void) {
      printf ("-------------------------------------------------------------\n\n");
      readInFile();
      printf ("-------------------------------------------------------------\n");
      return 0;
    
    }
    
    void readInFile(void) {
    	  
      FILE *p; /*pipe stream*/
      char line[MAXLINE]; /*lines read*/
      char *searchPattern = NULL;
      const char initialPattern [] = "<description>.*</description>"; /*initilise search pattern */ 
      int lineCounter = 1;
      
      Headline *arrayOfStories[10];
      Headline story;
      int arrayCounter=0;
      story.title = NULL;
      story.link = NULL;
      story.description = NULL;
       
      
      if((p = popen("/usr/other/sdm/Bin/wget -q -O - http://homepages.ihug.com.au/~pooni/xml/test.xml","r")) == NULL) {
    
        fprintf(stderr, "cannot create pipe \n");
        exit(1);		
    
      }
      
      //printf ("%d", sizeof (line));
      
      //performs search  
      while(fgets(line, sizeof line, p) != NULL) { 	
      	
    	printf("Line %d: - - - - - - - - - - - - \n", lineCounter);
    	strcpy(searchType, "title");
    	searchPattern = (char *) malloc (sizeof (char) * (strlen (initialPattern) + 1));
    	strcpy(searchPattern, "<title>.*</title>");
    	printf("Searching for TITLE\n");
    	match(line,searchPattern, arrayOfStories, &story, &arrayCounter);	
    	
    	strcpy(searchType, "link");
    	strcpy(searchPattern, "<link>.*</link>");
    	printf("Searching for LINK\n");
     	match(line,searchPattern, arrayOfStories, &story, &arrayCounter); 	
    	
    	strcpy(searchType, "description");
    	strcpy(searchPattern, "<description>.*</description>");
    	printf("Searching for DESCRIPTION\n");
      	match(line,searchPattern, arrayOfStories, &story, &arrayCounter);
     	
    	lineCounter++;
    	printf ("\n"); 	
    	free(searchPattern);
    	
      }
        
      pclose(p);
    }
    
    int match(const char *string, char *pattern, Headline *arrayOfStories[], Headline *story, int *arrayCounter) {
    	
    	int error = 0;
        regex_t re;
        size_t a = 2;
        regmatch_t arrayOfMatches[1000];
        int counter=0;
    
        if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE) != 0) {
            return(0);      /* report error */
        }
        
    	/* this call to regexec() finds the first match on the line */
    	error = regexec (&re, string, a, arrayOfMatches, 0);
    	
    	/* while matches found */
    	while (error == 0) {    
    	
     		/* substring found between pm.rm_so and pm.rm_eo */
     		getHeadlines(arrayOfMatches[counter].rm_so, arrayOfMatches[counter].rm_eo, string,arrayOfStories,story,arrayCounter);
     		
      	  	/* This call to regexec() finds the next match */
        	error = regexec (&re, string + arrayOfMatches[counter].rm_eo, 1, arrayOfMatches, REG_NOTBOL);
        	
    		counter++;
        }
    	
    	regfree(&re);
        
        return(1);
    }
    
    
    char getHeadlines(int start, int end, const char *searchString, Headline *arrayOfStories[], Headline *story, int *arrayCounter){
    	
    	char *tagdata = ""; 	
    	char tempChar;
    	char STag[] = "<description>";
    	char ETag[] = "</description>";
    	int counter=0;
    	int sTagLength=0;
    	int eTagLength=0;
    	
    	if(strcmp(searchType,"title")==0) {
    		strcpy(STag, "<title>");
    		strcpy(ETag, "</title>");
    	}
    	
    	else if(strcmp(searchType,"link")==0) {
    		strcpy(STag, "<link>");
    		strcpy(ETag, "</link>");
    	}
    	
    	else {
    		strcpy(STag, "<description>");
    		strcpy(ETag, "</description>");		
    	}
    		
    	sTagLength = strlen(STag);
    	eTagLength = strlen(ETag);	
    	
    	start = start + sTagLength;
    	end = end - eTagLength;	
    	
    	tagdata = (char *)calloc((end-start+1), sizeof(char));
    	
    	while(start < end) {
    		
    		tempChar = searchString[start];
    		tagdata[counter] = tempChar;
    		
    		counter++;
    		start++;
    	}
    	
    	tagdata[counter] = '\0';
    	//printf("Results: ");
    	//printf("%s\n", tagdata);
    	
    	storeHeadlines(tagdata, story, arrayOfStories, arrayCounter);
    	
    	free(tagdata);
    }
    
    void storeHeadlines(char *tagdata, Headline *story, Headline *arrayOfStories[], int *counter) {	
    	
    	if(strcmp(searchType,"title")==0) {
    		(*story).title = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).title, tagdata);
    	}
    	
    	else if(strcmp(searchType,"link")==0) {
    		(*story).link = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).link, tagdata);		
    	}
    	
    	else {
    		(*story).description = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).description, tagdata);		
    	}
    	
    	if((*story).title != NULL && (*story).link != NULL && (*story).description != NULL) {
    		
    		*arrayOfStories = (Headline *) calloc (1, sizeof (Headline *));
    		*arrayOfStories[*counter] = (Headline)*story;
    		*counter++;
    		printHeadlines(counter, arrayOfStories);
    		free(*arrayOfStories);		
    	}
    	
    	
    	
    	free((*story).title);
    	free((*story).link);
    	free((*story).description);
    	story=NULL;	
    }
    
    void printHeadlines(int *counter, Headline *arrayOfStories[]) {
    	Headline *tempPtr;
    	Headline tempStory;
    	int i=0;
    	
    	for(; i<*counter; i++) {
    				
    		tempPtr = arrayOfStories [i];
    		tempStory = *tempPtr;
    		printf("TITLE %s \n LINK %s \n DESCRIPTION %s \n", tempStory.title, tempStory.link, tempStory.description);
    		
    	}
    		
    
    }
    thanks and i would appreciate any help in regards to how i should stop this segmentation fault

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Headline *tempPtr;
    >>tempStory = *tempPtr;
    Where is tempPtr pointing to when you try to dereference it? No-where special is the answer, hence the seg fault
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    please look at the function printheadlines ... tempPtr is pointing to something...

    tempPtr = arrayOfStories [i];
    tempStory = *tempPtr;

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's with your aversion to the arrow operator?
    Code:
    (*story).title
    And, in the same school of thought:
    Code:
    Headline *tempPtr;
    Headline tempStory;
    ...
    tempStory = *tempPtr;
    Why? There is no need for tempStory at all in your code. Oh, and as to why it's crashing, that's obvious. Because you're trying to access memory you don't have any business accessing. That's what a segfault is. It means usually means you're running off either end of your array, or are dereferencing some random point in memory.

    [edit]
    Oh, actually, do you want to know what your problem really is? Or at least one of them?
    Code:
    void storeHeadlines(char *tagdata, Headline *story, Headline *arrayOfStories[], int *counter) {	
    	
    	if(strcmp(searchType,"title")==0) {
    		(*story).title = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).title, tagdata);
    	}
    	
    	else if(strcmp(searchType,"link")==0) {
    		(*story).link = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).link, tagdata);		
    	}
    	
    	else {
    		(*story).description = (char *)malloc(strlen(tagdata)+1);
    		strcpy ((*story).description, tagdata);		
    	}
    	
    	if((*story).title != NULL && (*story).link != NULL && (*story).description != NULL) {
    		
    		*arrayOfStories = (Headline *) calloc (1, sizeof (Headline *));
    		*arrayOfStories[*counter] = (Headline)*story;
    		*counter++;
    		printHeadlines(counter, arrayOfStories);
    		free(*arrayOfStories);		
    	}
    	
    	
    	
    	free((*story).title);
    	free((*story).link);
    	free((*story).description);
    	story=NULL;
    }
    You've got all kinds of problems. Can you spot them now?
    [/edit]


    Quzah.
    Last edited by quzah; 04-19-2004 at 06:49 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic array segmentation fault
    By Argentius in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 04:50 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  4. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  5. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM

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