Thread: Odd Segmentation Fault

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Odd Segmentation Fault

    I wrote a program that includes the following code:

    Code:
    struct time     /* Structure for time variables */
    {
            int month;
            int day;
            int year;
    
            int hr;
            int min;
    };
    
    ...
    
            struct time time1,time2;
    
            printf("Start moment:  ");
            fgets(line,sizeof(line),stdin);
            sscanf
            (
                    line,
                    "%d/%d/%d %d:%d",
                    &time1.month, &time1.day, &time1.year, &time1.hr, &time1.min
            );
    The compiler (gcc) doesn't issue any warnings.

    As soon as I enter the start moment, the program terminates, like so:

    Code:
    greg@snapdragon:~/Programs/Oua/mindiff$ ./a.out
    Start moment:  8/12/1982 9:01
    Segmentation fault
    Interestingly, NO such segmentation fault occurs in the following cases:

    Code:
    struct date {
            int month;
            int day;
            int year;
    };
    
    ...
    
            printf("Date 1:  ");
            fgets(line,sizeof(line),stdin);
            sscanf(line,"%d/%d/%d",&date1.month,&date1.day,&date1.year);
    There is also no problem at all with this code:

    Code:
    struct time {
            int hr;
            int min;
    };
    
    ...
    
            printf("Time 1:  ");
            fgets(line,sizeof(line),stdin);
            sscanf(line,"%d:%d",&time1.hr,&time1.min);
    I thought the problem might be the space, and tried
    Code:
            printf("Start moment:  ");
            fgets(line,sizeof(line),stdin);
            sscanf
            (
                    line,
                    "%d/%d/%d,%d:%d",
                    &time1.month, &time1.day, &time1.year, &time1.hr, &time1.min
            );
    (with a comma instead of a space in the input), but that didn't help; still got a segmentation fault!

    I'm completely baffled. Does anybody know what is causing the problem?
    Last edited by JohnnyAppleseed; 08-24-2010 at 09:25 AM. Reason: Thought of something I should have mentioned.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And what is line?

    char *line;
    perhaps?

    What about
    char line[BUFSIZ];
    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Actually, line is just like you suggest:
    Code:
    char line[15];

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I can't see anything wrong there.

    What else is there in all the ... stuff you've deleted?

    Can you post a minimal and complete example that still shows the problem?
    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.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Not sure what you mean by "minimal and comlete example," but would just posting the entire code help?

    Code:
    /************************************************
    *	A program to calculate the difference	*
    *	between two times, in minutes.		*
    *************************************************/
    
    /* I/O and basic needs */
    
    #include <stdio.h>	/* I/O library */
    char line[15];		/* Input line */
    int temp;		/* For anywhere a temporary variable is needed */
    
    /* Basic structural definitions */
    
    struct time	/* Structure for time variables */
    {
    	int month;
    	int day;
    	int year;
    
    	int hr;
    	int min;
    };
    
    int modays[12]={31,0,31,30,31,30,31,31,30,31,30,31}; /* Month lengths */
    
    /* Function to obtain date value in days for easy subtraction */
    
    int dayval(struct time convdate)
    {
            int total;	/* Total number of days from 1/1/0 */
    
            total = convdate.year / 400 * 146097;	/* Calculation of days	*/
            convdate.year %= 400;			/* in previous years,	*/
            total += convdate.year / 100 * 36524;	/* accounting for leap	*/
            convdate.year %= 100;			/* years.		*/
            total += convdate.year / 4 * 1461;
            convdate.year %= 4;
            total += convdate.year * 365;
    
            if (convdate.year == 0) modays[1] = 29;	/* Number of days in	*/
            else modays[1] = 28;			/* Feb of current year	*/
    
            for (temp=0 ; temp < (convdate.month - 1) ; temp++)
            {					/* Calculation of days	*/
                    total += modays[temp];		/* in previous months	*/
            }
    
            total += convdate.day - 1;	/* Previous days in current month */
    
            return (total);
    }
    
    void main()
    {
    	struct time time1,time2;	/* Start and end declarations */
    
    	printf("Start moment:  ");
    	fgets(line,sizeof(line),stdin);
    	sscanf
    	(
    		line,
    		"%d/%d/%d %d:%d",
    		&time1.month, &time1.day, &time1.year, &time1.hr, &time1.min
    	);
    
    	
    	printf("End moment:  ");
    	fgets(line,sizeof(line),stdin);
    	sscanf				/* Get end moment */
    	(
    		line,
    		"%d/%d/%d %d:%d",
    		&time2.month, &time2.day, &time2.year, &time2.hr, &time2.min
    	);
    
    	/* Calculate difference */
    	int daydiff;
    	long int mindiff;
    
    	daydiff = dayval(time2) - dayval(time1);
    	mindiff = daydiff * 1440;	/* Day difference in minutes */
    	temp = (time2.hr - time1.hr) * 60 + time2.min - time1.min;
    	mindiff += temp;	/* Add difference within day */
    
    	printf("\nDifference in minutes:  %ld\n\n",mindiff);
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't you think 15 is too small?
    char line[15];
    to accept the input.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    WOW, that was it! I can't believe I made all this commotion over such a stupid error.

    Thank you, Bayint Naung! Thank you also to everyone who took the time to look at this.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > "minimal and comlete example,"
    One which we can simply copy/paste and see for ourselves.

    In your case, it wasn't the short buffer per se that was the problem.

    It was
    a) NOT checking the return result of sscanf to check that your conversion was a success.

    b) Using the resulting non-assigned values in your struct to control a loop iterating over an array.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. odd segmentation fault occurence
    By gazsux in forum C Programming
    Replies: 6
    Last Post: 12-13-2002, 09:02 AM

Tags for this Thread