Segmentation Fault 11 in Flipifneeded Function

  • Thread starter charmedbeauty
  • Start date
  • Tags
    Fault
In summary, the code is causing a segmentation fault when input is entered. The code is meant to flip pieces on a board, but there are several issues with the code, including not protecting against going outside the bounds of the array, using = instead of ==, and not having proper compiler warnings set. These issues need to be fixed in order for the code to run properly.
  • #1
charmedbeauty
271
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
  • #2
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 ==.
 

1. What does "Segmentation fault 11" mean?

A "Segmentation fault 11" error is a type of runtime error that occurs when a program attempts to access a memory address that is not allocated to it. This can happen due to a variety of reasons, such as a bug in the code, improper memory management, or trying to access data that has been deleted.

2. How do I fix a Segmentation fault 11 error?

The first step in fixing a Segmentation fault 11 error is to identify the source of the error. This can be done by debugging the code and checking for any memory-related issues. You can also try using a memory debugger tool, such as Valgrind, to pinpoint the exact location of the error. Once you have identified the source, you can then make the necessary changes to your code to prevent the error from occurring.

3. Can a Segmentation fault 11 error cause data loss?

No, a Segmentation fault 11 error itself does not cause data loss. However, if the error is not handled properly, it can lead to unexpected program behavior and potentially corrupt data. It is important to fix the error as soon as possible to prevent any potential data loss.

4. Why does my program only sometimes produce a Segmentation fault 11 error?

A Segmentation fault 11 error can be unpredictable and may only occur under certain conditions. This could be due to a specific input or data that triggers the error, or it may be caused by a memory issue that is not consistent across different runs of the program. It is important to thoroughly test your code and handle any potential memory issues to prevent this error from occurring.

5. Is a Segmentation fault 11 error specific to a certain programming language?

No, a Segmentation fault 11 error can occur in any programming language that allows direct memory access, such as C or C++. It is not specific to any particular language, but rather a result of improper memory management in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Atomic and Condensed Matter
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
Back
Top