1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation fault in porgram

Discussion in 'C' started by rishi123, Nov 25, 2007.

  1. rishi123

    rishi123 New Member

    Joined:
    Nov 25, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Below is the program for sorting the file in ascending and reverse order
    this program is working properly under window but it gives segmentation fault under unix
    Can u find where i m mistaking.... :(
    u can give command line arguments like this

    ./myprogram -a filename.txt for sortin filename in ascending order
    ./myprogram -r filename.txt for sorting filename in reverse order

    Program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define BUFFER_LEN 100  /* Length of input buffer    */
    #define NUM_P 100       /* maximum number of strings */
    #define TRUE  1
    #define FALSE 0
    
    void main(int argc, char *argv[])
    {
       FILE *fp;
       char buffer[BUFFER_LEN];    /* space to store an input string    */
       char *pS[NUM_P] = { NULL }; /* Array of string pointers          */
       char *pTemp = NULL;         /* Temporary pointer                 */
       int i = 0, a=0;             /* Loop counter                      */
       int sorted = FALSE;         /* Indicated when strings are sorted */
       int last_string = 0;        /* Index of last string entered      */
       
       if(strcmp(argv[1],"-a")==0)
       a=1;
       if(strcmp(argv[1],"-r")==0)
       a=2;
    
       fp =fopen(argv[2],"r"); /* Opening the file in read mode     */
    
       while((*fgets(buffer,(BUFFER_LEN-1),fp) != '\0') && (i < NUM_P))
       {
         pS[i] = (char*)malloc(strlen(buffer) + 1);
         if(pS[i]==NULL)           /* Check for no memory allocated     */
         {
           printf(" Memory allocation failed. Program terminated.\n");
           return;
         }
         strcpy(pS[i++], buffer);
       }
       last_string = i;            /* Save last string index            */
    
       switch(a)
       {
       case 1:
        {
         /* Sorting in ascending order */
          while(!sorted)
          {
    	sorted = TRUE;
    	for (i = 0 ; i < last_string - 1 ; i++)
    	  if(strcmp(pS[i], pS[i + 1]) > 0)
    	  {
    	    sorted = FALSE;     /* We were out of order */
    	    pTemp = pS[i];      /* Swap pointers pS[i]  */
    	    pS[i] = pS[i + 1];  /*       and            */
    	    pS[i + 1] = pTemp;  /*     pS[i + 1]        */
    	  }
          }
          break;
        }
       case 2:
        {
         /* Sorting in decending order */
          while(!sorted)
          {
    	sorted = TRUE;
    	for (i = 0 ; i < last_string - 1 ; i++)
    	  if (strcmp(pS[i], pS[i + 1]) < 0)
    	  {
    	    sorted = FALSE;     /* We were out of order */
    	    pTemp = pS[i];      /* Swap pointers pS[i]  */
    	    pS[i] = pS[i + 1];  /*       and            */
    	    pS[i + 1] = pTemp;  /*     pS[i + 1]        */
    	  }
          }
          break;
        }
       default:
        {
         printf("wrong argument passed");
         exit(1);
        }
       }
       /* Displayed the sorted strings */
       printf("\nYour input sorted in order is:\n\n");
       for (i = 0 ; i < last_string ; i++)
       {
         printf("%s\n", pS[i] );
         free( pS[i] );
         pS[i] = NULL;
       }
    }
     
    Last edited by a moderator: Nov 25, 2007
  2. SpOonWiZaRd

    SpOonWiZaRd Know what you can do.

    Joined:
    May 30, 2007
    Messages:
    747
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Network Engineer/Programmer
    Location:
    South Africa
    This is the Introduce yourself section....
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Moved to C-C++ forum.
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    1. main returns int, not void

    2. (*fgets(buffer,(BUFFER_LEN-1),fp) != '\0')
    When fgets() reaches the end, it returns NULL.
    Then when you try to dereference that result, you segfault.
    The correct loop condition is
    fgets(buffer,(BUFFER_LEN-1),fp) != NULL

    Also, you don't have to account for the \0 in your count, so you can just do
    fgets(buffer,BUFFER_LEN,fp) != NULL
     

Share This Page