Thread: Seg fault problem

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    13

    Question Seg fault problem

    When I run this program I get a seg fault when ever I run it and it happen on the last pass through for loop. I put in print statements at one point just to see where the errors were happening and it would go all the way through the for loop and when i=25 it would seg fault at the first assignment. The main thing that confuses me is that it does this no matter what I make the second argument for the for loop.

    Code:
    int main(int argc, char* argv[])
    {
    struct LIST map[26];
    int i;
    
    for(i=0;i<26;i++){
    map[i].startingPoint=' ';   
    map[i].first=NULL;
    }
    if(argc>1){
      fillStruct(argv[1],map);
      printList();
      depthSearch();
      breadth();
      topoSort();
    }
    else
    printf("No file to print");
    return 0;
    }
    
    
    int main(int argc, char* argv[])
    {
    struct LIST map[26];
    int i;
    
    for(i=0;i<10;i++){
    printf("%d \n",i);
    map[i].startingPoint=' ';   
    printf("%d set to blank",i);
    map[i].first=NULL;
    printf("%d set to NULL",i);
    }
    if(argc>1){
    printf("Send to fillstruct");
      fillStruct(argv[1],map);
      printList();
      depthSearch();
      breadth();
      topoSort();
    }
    else
    printf("No file to print");
    return 0;
    }
    Any help will be greatly appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You won't know how far you get unless you fflush(stdout) after every call to printf. I believe it to be far more likely that it's dying on one of the calls inside the if(argc>1) block.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So use a debugger.

    gdb a.out
    // some output from GDB
    run filename.txt
    // some output from program, plus a segfault
    bt
    // find out where exactly you got to.


    From that point, you can examine variables to see what is wrong (typically a pointer containing NULL or garbage).
    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. seg fault with any function call
    By LegoMan in forum C Programming
    Replies: 5
    Last Post: 04-15-2009, 05:30 PM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  4. unknown seg fault reason.
    By egoveneror2 in forum C Programming
    Replies: 16
    Last Post: 04-15-2008, 02:30 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM