Thread: segmentation fault with strtod

  1. #16
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Gdb points to this code where its segmentation faulting im pretty sure its not buffer overrun i fixed that already


    Code:
     clock[hr[m]][hc[m]] = ms;
     clock[hr[m]][hc[m]+ 1] = ms;
    Code:
    int displayClock(int * optionStats, int h, int m)
    {
        char clock[9][20] = { /* The array to hold the hard-coded clock */
            "        12         ",
            "   11        01    ",
            " 10            02  ",
            "                   ",
            "09      .       03 ",
            "                   ",
            " 08            04  ",
            "   07        05    ",
            "        06         "
          };
    
    	/* Row and column numbers in clock array for hour positions. For
    	 * example, the "06" in the clock display is stored at position
    	 * [8, 8] in the clock matrix (see above) */
        const int hr[13] = {0, 1, 2, 4, 6, 7, 8, 7, 6, 4, 2, 1, 0};
        const int hc[13] = {0, 13, 15, 16, 15, 13, 8, 3, 1, 0, 1, 3, 8};
    
    	/* Hour and minute codes */
        const char hs = 'H', ms = 'm';
    
        int i, j; /* Declare other variables as needed */
        int a, b;
    	/* The next 5 comments require code to be developed by you! */
    
    	/* Ensure that h and m are in range and, if not, return
    	 * appropriate error value. Otherwise adjust h if it is 0 or it
    	 * exceeds 12
    	 */
         if(h>12 || h<1)
         {
                printf("Hours is invalid 1 - 12\n");
                return FALSE;
         }
             
    
         if(m<0 || m>=60)
         {
                printf("Minutes is invalid 0 - 60\n");
                return FALSE;
         }
             
        /* Now update the clock array accordingly, with the hour symbol */
    
    	/* Round m to the nearest hour position */
    
    	/* Update clock with minute symbol, taking into account a "clash"
    	 * with the hour symbol 
    	 */
              for(a=0; a<9; a++)
            {
            
              for(b=0; b<19; b++)
              {
                     clock[hr[h]][hc[h]] = hs;
                     clock[hr[h]][hc[h]+ 1] = hs;
                   
                  
                      
                    
                   
               }
                     
                     clock[hr[m]][hc[m]] = ms;
                     clock[hr[m]][hc[m]+ 1] = ms;
             }     
    
        
    	/* Display the clock */
        for (i = 0; i < 9; i++)
        {
            for (j = 0; j < 19; j++)
        	    printf("%c", clock[i][j]);
            printf("\n");
        }
        printf("\n");
    
        return 0;
    }
    Code:
    int getClockInput(int optionStats[NUM_OPTION_STATS])
    {
        int h, m;
        int display_error, end_of_input;
        char  start[50];
        char *end;
    
        printf("\nEnter <hours> <minutes>: ");
    
        fgets(start, sizeof start, stdin);
        h = (int) strtol(start, &end,10);
        m = (int) strtol(end, NULL,10);
    
        if(start[strlen(start)-1] !='\n')
        {
              readRestOfLine();
    
        }
        else
        {
          start[strlen(start)-1] = '\0';
        }
        /*scanf("%d%d", &h, &m);*/
        display_error = displayClock(optionStats,h, m);
        
        if (display_error)
        {
          printf("Incorrect data received for hours and minutes\n");
          return 0;
        }
    
        return 0;
    
    }

  2. #17
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    hr[m] and hc[m] is still invalid, since m goes from 0 to 59 and hr/hc is only of size 13 ...

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't see what in this loop
    Code:
    for(b=0; b<19; b++)
              {
                     clock[hr[h]][hc[h]] = hs;
                     clock[hr[h]][hc[h]+ 1] = hs;
                   
                  
                      
                    
                   
               }
    depends on b
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    so how would i fix up the array so that it prints 'm' for minutes when it displays the clock?

  5. #20
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    never mind guys i fixed it by rounding the minutes by 5 then updating the mintes array with the result of the rounded number

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM