Segmentation Fault printing an array of structs

This is a discussion on Segmentation Fault printing an array of structs within the C Programming forums, part of the General Programming Boards category; hey everyone, im getting a Segmentation Fault when i run my code... and i found where it is happening by ...

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

    Segmentation Fault printing an array of structs

    hey everyone,

    im getting a Segmentation Fault when i run my code... and i found where it is happening by debugging it...its in the printHeadlines(); function, which is the last function in this code below. all that function is doing, is printing an array of Headlines structures.

    the code is below and thanks for any help...

    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[1];
      Headline story;
      int arrayCounter=0;
       
      
      if((p = popen("/usr/other/sdm/Bin/wget -q -O - http://homepages.ihug.com.au/~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 *)malloc (1);
    		*arrayOfStories[*counter] = (Headline)*story;
    		*counter++;
    		printHeadlines(counter, 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;
    		try printf ("%p", tempPtr) and see what the output is
    		printf("TITLE %s \n LINK %s \n DESCRIPTION %s \n", tempStory.title, tempStory.link, tempStory.description);
    		
    	}
    		
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > searchPattern = (char *) malloc (sizeof (char) * (strlen (initialPattern) + 1));
    I see no point in calling malloc/free inside the loop, just to store the same amout of data each time
    You may as well use the initalPattern array, and just strcpy() in the pattern you want to match against

    > Headline *arrayOfStories[1];
    You almost certainly want more that ONE pointer here

    > *arrayOfStories = (Headline *)malloc (1);
    This allocates ONE byte, not enough for a whole Headline

    The general template for calling malloc is
    p = malloc ( number * sizeof *p );

    Which if you're after just one thing is
    p = malloc ( sizeof *p );



    Stop casting malloc as well, it doesn't help at all in ANSI-C
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05:50 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 03:58 PM
  4. Replies: 3
    Last Post: 04-19-2004, 07:42 PM
  5. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 01:36 PM

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