Thread: Segfault

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Segfault

    I can't for the life of me figure out why this keeps giving me a segfault, but I'm pretty sure it has something to do with the test--;

    The program is supposed to solve sudoku puzzles and this portion of my code is supposed to use a brute force approach to solve the sudoku once all single candidates have been found. Basically, it runs through each empty square from left to right starting from the top row and ending at the bottom row. If all 9 numbers cannot be legally placed in an empty square, it will backtrack to the last empty square and increment its value. If that square holds a 9, it will clear it and move back to another empty square.

    Any suggestions?

    Code:
       if (escount > 0)
       {
          test = 0;
          do
          {
             num = puzzle[row][col] + 1;
             find = 0;
             row = es_row[test];
             col = es_col[test];
    
             do
             {
                if (check_valid(puzzle, row, col, num) == 1)
                {
                   puzzle[row][col] = num;
                   attempt++;
                   find++;
                   break;
                }
                else
                {
                   num++;
                }
             }
             while (num < 10);
    
             if (find == 0)
             {
                test--;
                back++;
                row = es_row[test];
                col = es_col[test];
    
                while (puzzle[row][col] == 9)
                {
                   puzzle[row][col] = 0;
                   test--;
                   back++;
                   row = es_row[test];
                   col = es_col[test];
                }
             }
             else
             {
                test++;
             }
    
          }
          while (test < escount);
       }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hard to say from that code-segment, but I would expect that you are getting a value out of range somewhere. Check your count up/down places, and make sure that you are not going past the size of the array anywhere [or going negative].

    Segfault is caused by addressing memory that isn't owned by you, and 99.999% of the time it's caused by either using un-initialized pointer or indexes outside the range allowed for an array and other coding problems. The other 0.001% is caused by compiler bugs or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    It may be that test = -1, if find = 0. Put in some print statements to show what is happening in your program when you run it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 10:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM