Thread: Segmentation fault

  1. #16
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yeah this is a nice place to find answers to your questions even if you have been programming in C for years and find yourself scratching your head.

    Also, regarding the nights spent programming, if you are passionate about this trust me, not much will change after college.

  2. #17
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    ok so at that point in my code I am still getting tokens but then the program hangs and I have to stop it, here's what gdb gives me:

    Code:
    **********************
    * TESTING opcodeincr *
    **********************
    COP 3601: Spring, 2010 - 
    
    
    Enter breakup line (enter done to exit test) : EXAMPLE +LDA STUFF,X
    
    
    STUFF,X
    
    
    Program received signal SIGINT, Interrupt.
    0x00000034c3470cb9 in strcmp () from /lib64/tls/libc.so.6
    (gdb) quit
    So I don't think strtok() is the issue, I'm probably wrong though. I was having problems earlier with my breakup.c hanging up like that but I thought I had fixed the issue.

  3. #18
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Quote Originally Posted by cas View Post
    The obvious candidate in this piece of code is the index into codetable; if it's out of range you could get a segfault. I suppose you had to comment that out, though, when you commented out the codetable declaration.

    When you get a segfault, your first step should be to use a debugger to pinpoint where the problem is, if not why it's occurring. Two good, free debuggers are Valgrind and gdb. They approach debugging in different manners, and each has its uses, but I tend to find Valgrind more useful.

    Gdb should tell you where the segfault is happening. Valgrind might be able to tell you where the actual problem is: where a segfault occurs is not necessarily where your bug is. Either way, learning how a debugger works should be one of the first things you do when leaning how to program. After all, creating bugs is also one of the first things you do!
    Unfortunately the server I am working on does not have valgrind

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So how is strcmp getting a bad string, then? (Interestingly, I don't see a call to strcmp anywhere in your code.) I would still check all your return values from strtok.

  5. #20
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I agree with tabstop! No calls from strcmp whatsoever. Can you printf %s of ptr after the last strtok before the crash?

  6. #21
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    I believe the strcmp() is being called in the driver routine that is provided by the professor which would explain it's mysteriousness haha. I will have to delve a bit deeper to determine if that is the case or if it is maybe being called in yet another one of my subroutines, I'll get back to you on that.

  7. #22
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Interestingly I'm getting another error now, not sure what caused it but it looks like it's coming from another subroutine, so I will post it and try to see what it may be causing. Thanks for bearing with me and my monstrous stack of code, don't worry I hate looking at it all too

    Code:
    **********************
    * TESTING opcodeincr *
    **********************
    COP 3601: Spring, 2010 - 
    
    
    Enter breakup line (enter done to exit test) : EXAMPLE +LDA STUFF,X
    
    
    ptr after getting "opcode":LDA
    
    
    
    ptr after "removing white space":STUFF,X
    
    
    Program received signal SIGINT, Interrupt.
    findfirst (tabl=0x5042e0, n=57, x=0x7fbffff7b0 "LDA") at findfirst.c:28
    28            mid = (min + max) / 2;
    (gdb) quit
    findfirst.c subroutine
    Code:
    /* Pre-Processor Directives */
    #ifndef SYMBSIZE
       #define SYMBSIZE 10
    #endif
    
    typedef struct tablemem
    {
       char symbol[SYMBSIZE];
       int value;
       int casenmbr;
       int otherinfo;
    } tabletype;
    
    /*
    * Finds the first occurrence of a symbol from the table using a binary search
    * and returns its index, otherwise returns -1 if not found.
    */
    int findfirst(tabletype tabl[], int n, char x[])
    {
       int index = -1;
    
       int min = 0;
       int max = n;
       int mid = n/2;
    
       while(min <= max)
       {
          mid = (min + max) / 2;
          if(strcmp(tabl[mid].symbol, x) < 0)
             min = mid + 1;
          else
             max = mid;
    
          if(strcmp(tabl[mid].symbol, x) == 0)
             index = mid;
       }
    
       return index;
    }

  8. #23
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Ok, based on that error now I'm starting to think that it's hanging up because it can't find the symbol it's looking for (in this case it should be looking for LDA, not +LDA) because there is a problem with my extern declaration of the codetable, what do you guys think?

  9. #24
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    mid = (min + max) / 2;

    Where is that line?

    Nevermind, I'm an idiot lol

  10. #25
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you post printfs of tabl[mid].symbol inside the while loop as well as char x[]?

  11. #26
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Well there's definitely an infinite loop there

    From:
    Code:
    int findfirst(tabletype tabl[], int n, char x[])
    {
       int index = -1;
    
       int min = 0;
       int max = n;
       int mid = n/2;
    printf("\n\n%s\n\n", x);
       while(min <= max)
       {
          mid = (min + max) / 2;
    printf("%s\n\n", tabl[mid].symbol);
          if(strcmp(tabl[mid].symbol, x) < 0)
             min = mid + 1;
          else
             max = mid;
          
          if(strcmp(tabl[mid].symbol, x) == 0)
             index = mid;
       }
       
       return index;
    }
    Testing:
    Code:
    **********************
    * TESTING opcodeincr *
    **********************
    COP 3601: Spring, 2010 - 
    
    
    Enter breakup line (enter done to exit test) : EXAMPLE +LDA STUFF,X
    We get:
    Code:
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    
    LDA
    Continues like that forever of course

    Taking out the printf for the symbol we get this:
    Code:
    **********************
    * TESTING opcodeincr *
    **********************
    COP 3601: Spring, 2010 - 
    
    
    Enter breakup line (enter done to exit test) : EXAMPLE +LDA TEST,X
    
    
    ptr after getting "opcode":LDA
    
    
    
    ptr after "removing white space":TEST,X
    
    
    
    char x:LDA
    
    
    Program received signal SIGINT, Interrupt.
    0x0000000000400d2e in findfirst (tabl=0x5042e0, n=57, x=0x7fbffff7b0 "LDA")
        at findfirst.c:28
    28            mid = (min + max) / 2;
    (gdb) quit

  12. #27
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Also, I got a response from my professor just now to my email which reads:

    Since codetabsize is unused in your code, it is not the problem. Assuming the segmentation fault is in opcodeincr, the more likely candidate is the value passed in through tablindx or possibly xbpe, which is a pointer to integer in breakup.

    From: Josh Williamson
    Sent: Thursday, March 25, 2010 09:23 pm
    To: Professor
    Subject: Segmentation fault

    Could you tell me why I'm getting a segmentation fault from the following code? I haven't done any actual processing so I think it has to do with the extern statements. I have the codetable in my working folder so I think it specifically has to do with the codetabsize integer. Please help, I finally got my breakup.c working correctly.
    Like I said, he's pretty cryptic haha

  13. #28
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Aight, give me a minute to take a look at it.Your stuff is due tomorrow right?

  14. #29
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Oh and I sent this response back to him to hopefully clarify that we're at least looking in the right direction

    So the issue would be the tablindx if there is an issue with my findfirst and it would be the xbpe if there is an issue with my breakup, correct? I'm attempting to narrow it down using dbg and I think it may be a problem in my findfirst where it is getting into an infinite loop when trying to find the symbol. I am fairly certain I am getting the codetable correctly passed to the correct subroutines but that may be it. Sound possible or am I looking in the wrong direction completely?
    And yes it is due tomorrow noon EST but I'm not too worried about getting it turned in on time, he's really lenient on late points, I realistic have until Sunday at noon before he'll start docking me (probably the best quality he has lol, that and he has tons of cool stories about back in the day of IBM and the 7 dwarves).

  15. #30
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Haha... Yeah I had a prof like that in college. He always kept telling us the "pizza story" which was a story of how he calculated how long his son's computer program would take to run, took the kid for pizza and came back in time to get the results . He was old and quirky but he was my favorite prof in college which is why I still remember this.

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