Segmentation Fault 11 in Flipifneeded Function

  • Thread starter Thread starter charmedbeauty
  • Start date Start date
  • Tags Tags
    Fault
Click For Summary
SUMMARY

The segmentation fault issue in the provided C code occurs within the flipifneeded function, specifically due to improper boundary checks for the array indices x and y. The code attempts to access elements outside the defined bounds of the array, leading to runtime errors. Additionally, there is a critical mistake in the line while(array[x-=deltax][y-=deltay] = WHITE), where the assignment operator = is incorrectly used instead of the equality operator ==. To resolve these issues, implement proper boundary checks and correct the operator usage.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of multi-dimensional arrays
  • Debugging techniques for runtime errors
  • Compiler warning settings and their importance
NEXT STEPS
  • Implement boundary checks for array indices in C programming
  • Learn about compiler warning levels and how to enable them
  • Study the differences between assignment and equality operators in C
  • Explore debugging tools for C, such as GDB, to trace segmentation faults
USEFUL FOR

Software developers, particularly those working with C programming, and anyone troubleshooting segmentation faults in their code will benefit from this discussion.

charmedbeauty
Messages
266
Reaction score
0

Homework Statement



when I am compiling my code I am getting a segmentation fault when entering in input but I don't know why?

it prints Segmentation fault: 11



Homework Equations





The Attempt at a Solution



This is my code I tried looking up what was causing it but couldn't find the reason please help

Code:
void flipifneeded(int array[][MAX_SIZE], int posx, int posy, int turn){

    int x,y,i;
    int deltax = 0;
    int deltay = 0;
 
  
    //down

   while(turn == 1){

      for(deltax = -1; deltax< 2; deltax++){
         for(deltay = -1; deltay< 2; deltay++){

           // deltax = deltax + posx;
           // deltay = deltay + posy;

            if(array[deltax + posx][deltay + posy] == BLACK){
               x = deltax + posx;
               y = deltay + posy;

               for(i = 0; i< 10000; i++){ x += deltax;
                  y += deltay;

                  if((x<0 || x >= MAX_SIZE || y < 0 || y>= MAX_SIZE) ||
                  (array[x][y] == EMPTY)) {
                     break;
                  }
                  if(array[x][y] == BLACK){
                     while((array[x-=deltax][y-=deltay] == WHITE)){
                        array[x][y] = BLACK;
                     }
                     break;

                  }
               }
            }
         }
      }
   }
   while(turn == 2){

      for(deltax = -1; deltax< 2; deltax++){
         for(deltay = -1; deltay< 2; deltay++){

            if(array[deltay + posy][deltax + posx] == WHITE){
               x = deltax + posx;
               y = deltay + posy;

               for(i = 0; i< 10000; i++){

                  x += deltax;
                  y += deltay;

                  if((x<0 || x >= MAX_SIZE || y < 0 || y>= MAX_SIZE) ||
                  (array[x][y] == EMPTY)) {
                     break;
                  }

                  if(array[x][y] == BLACK){
                     while(array[x-=deltax][y-=deltay] = WHITE){
                        array[x][y] = BLACK;

                       }
                     break;
                  }
               }
            }
         }
      }
   }
}

and this is the function I call it from


Code:
int playingame(int array[][MAX_SIZE],int turn){
  // char input[2];

   int posx=0;
   int posy=0;
   if((turn == 2)){

      printf("it is Black's turn to move\n");
   }

   else{

      printf("whites turn\n");
   }

   char letter=0;
   int number=0;
   printf("Enter your move: \n");
   letter = getchar();
   number = getchar();
   getchar();

   posy = letter -'a';
   posx = number - '0'-1;

   //int counti = i;
   //int countj = j;


   if((array[posx][posy] == CROSS && turn == 2)){
      printf("error\n");
      turn = 2;
   }
   else if((array[posx][posy] == CROSS && turn == 1)){
      printf("error\n");
      turn = 1;
   }
   if((array[posx][posy] == EMPTY && turn == 2 )){

 array[posx][posy] = BLACK;

      updateboard(array);
      flipifneeded(array,posx, posy, turn);
      turn = nexturn(array, turn);

   }
   else if((array[posx][posy] == EMPTY && turn == 1)){

      array[posx][posy] = WHITE;
      updateboard(array);
      flipifneeded(array, posx, posy, turn);
      turn = nexturn(array, turn);

   }
   

   return turn;
   }

if someone could help me with this that would be great as I am in a bit of a hurry and can't proceed until I fix this issue.

THANKS.!
 
Physics news on Phys.org
Code:
                     while(array[x-=deltax][y-=deltay] = WHITE){
                        array[x][y] = BLACK;
                       }
There are at least three things wrong here.
  • You aren't protecting against x and y going outside the bounds of the array.
  • You should have used == WHITE rather than = WHITE.
  • You don't have your compiler warnings set high enough; a good compiler will warn you about using = where it looks like you should have used ==.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K