Why do I get segmentation errors ONLY for one specific input?


Sometimes instead of a segmentation fault, I get this error:
 *** glibc detected *** ./battleship: free(): invalid pointer: 0x0000000016983030 ***


I made a battleship program. In a nutshell, my board is an array of 10 characters by 10 characters. The assemble function creates the board such that the ships are aligned in straight lines and do not cross over each other.

In the main function, I prompt the user for the number of ships and the ship size. My board output is consistent for every input integar (less than 10) that is not 6. I don't know why 6 gives me the error because sizes 7,8, and 9 work okay.

My code shows the main function and the assemble function. The output should show the user the battleship board of all dots and an "S" where a ship is present. I haven't made the function yet that allows the user to bomb the board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# include<iostream>
# include<cstdlib>
# include<ctime>
# include<vector>
using namespace std;

const int rows=10;
const int cols=10;

void assemble(char grid[rows][cols], int sizeOfShip,int numOfShips)

/*For loop fills board with little dots */
for (int i=0;i<rows;i++)
{
   for (int j=0;j<cols;j++)
   {
      grid[i][j]='.'; 
   }
}
/* For loop counts every ship that is declared */
for (int k=0;k<numOfShips;k++) 
{

   int a,b,dx,dy; // initial ship locations and directions

   do //loop picks a random initial location,x=a and y=b
   {
   srand(time(NULL));
   a=rand()%cols;
   b=rand()%rows;
   }
   while (grid[b][a]=='S');

// Ships are represented by vectors on the board

   vector<int> x(sizeOfShip);	 
   vector<int> y(sizeOfShip);	
   x[0]=a;
   y[0]=b;
   bool goodDirection; //variable determines if the direction works
   bool inverted=false; //you'll understand the purpose of this variable in a later loop

   int n=0;	//integar n is the index of vectors x and y
   do
   {
      goodDirection=true;

   /*Finds the orientation of the ship */
      do
     {
         srand(time(NULL));	
         dx=-1+rand()%3;	//dx and dy are random directions
         dy=-1+rand()%3;
      }	
      while (grid[y[n]+dy][x[n]+dx]=='S' or (dx==0 and dy==0) or x[n]+dx<0 or x[n]+dx>cols-1 or y[n]+dy<0 or y[n]+dy>rows-1);	//while the new orientation isn't valid

      /* While the orientation is good */
      while (goodDirection==true and n<sizeOfShip)
      {	
         x[n+1]=x[n]+dx;
         y[n+1]=y[n]+dy;
         n=n+1;
         /*The direction fails and has not yet been inverted */
         if ((grid[y[n]][x[n]] =='S' or x[n]<0 or x[n]>cols-1 or y[n]<0 or y[n]>rows-1) and inverted==false)
         {	
            dx=-dx;
            dy=-dy; //go the opposite direction for efficiency
            inverted=true;
            x[n]=a+dx;
            y[n]=b+dy; //tries opposite orientation from the starting point
         }
         /*The direction fails and was already inverted */
         if ((grid[y[n]][x[n]] =='S' or x[n]<0 or x[n]>cols-1 or y[n]<0 or y[n]>rows-1) and inverted==true) 
         {
            goodDirection=false;
            n=0;	
            inverted=false;
         }
      } // end while loop for if the direction is good and the size of ship is less than sizeOfShip
   } 
   while (goodDirection==false); 

/*Assuming that every point of the ship along a straight line is valid and we can print out the ship*/
   for (int s=0;s<sizeOfShip;s++)
   {
      grid[y[s]][x[s]]='S';
   }
}	// end for loop that counts every ship


return;
}


int main()
{
   char board[rows][cols]; // ten by ten board
   int nShips; // number of enemy ships to bomb
   int shipSize; // size of enemy ship
   cout<<"Number of enemy ships? "<<endl;
   cin>>nShips;
   cout<<endl<<"Size of enemy ship? "<<endl;
   cin>>shipSize;
   cout<<endl;

   assemble(board,shipSize,nShips);	

/*Print the board */
   for (int i=0;i<rows;i++)
   {
      for (int j=0;j<cols;j++)
      {
         cout<<board[i][j];
      }
      cout<<endl;
   }
   cout<<endl;

   return 0;
}
Last edited on
closed account (DSLq5Di1)
1
2
3
4
   vector<int> x(sizeOfShip);
   vector<int> y(sizeOfShip);

   int n=0;	//integar n is the index of vectors x and y 

1
2
3
4
5
6
      /* While the orientation is good */
      while (goodDirection==true and n < sizeOfShip)
      {	
         x[n+1]=x[n]+dx; // Access violation here on the last iteration.
         y[n+1]=y[n]+dy; // ^
         n=n+1;
Topic archived. No new replies allowed.