+ 1

Error in Word Search Grid program

I have to find the word in a dynamic 2d array grid. First it will take the input from user if the word matches in dictionary file then it will search the word in array to find it locations. i have done most of the things but can't figure out why program is crashing. The problem is in board_search function i guess. My code is below https://code.sololearn.com/ce4SWZn65rM3/?ref=app

24th Jun 2019, 12:07 PM
Zain Asif
Zain Asif - avatar
7 Réponses
+ 2
~ swim ~ its 0 because i am auto growing the number of rows throw file you can see the row grow function and also char[20] is only to take name from user
24th Jun 2019, 12:33 PM
Zain Asif
Zain Asif - avatar
+ 1
I'm afraid to mention that the program oozing memory! After Line 79 you'd place two blocks of deallocation like for (int i = 0; i< number_of_row; ++i) delete [] board[i]; delete [] board; for (int i = 0; i< number_of_row; ++i) delete [] dictionary[i]; delete [] dictionary; to dismiss the utilized resources. Also, an infinite loop will take place as the result of the following query cout << endl << "Try again or -1 to exit: "; cin >> exit; because '\n' character after hitting the Enter, remains in the stream and next iterations can't consume it or discard it. It must be eliminated explicitly by 1) resetting the failed istream flag 2) flushing the istream from remainings in other words, putting these lines right after while loop (line 56) cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');
24th Jun 2019, 1:48 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
To Seek Glory in Battle is Glorious its kind of working but on running program multiple times its start crashing do you think i should delete memory anywhere else? Also i have removed that infinite loop
24th Jun 2019, 1:58 PM
Zain Asif
Zain Asif - avatar
+ 1
If you're not sure about the existence of "board.txt" and "dictionary.txt" or perhaps somehow the opening process of them has been failed, then make sure you catch it by checking the `failbit` flag after each `fin.open()`. if (fin.fail()) { cout << "File does not exist!\n"; }
24th Jun 2019, 2:06 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
To Seek Glory in Battle is Glorious bro the problem is with board_search function. It crashes after when it reaches that function. Its for university project if you could help me it would be really great. I have made some changes to the code. Here you can see it https://code.sololearn.com/ce4SWZn65rM3/?ref=app
24th Jun 2019, 2:18 PM
Zain Asif
Zain Asif - avatar
+ 1
Line 94 causes a major problem there. It literally blows the hell out of the program! You mustn't delete the passed pointer to pointer of char (a.k.a `board`) in the middle of the road. I even suspect the `row_grow()` done more harm than good, and you might even consider restructuring the implementation of `row_grow()`.
24th Jun 2019, 2:25 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
Please think about this unit. char **row_grow(char **arr, int &rows) { char **temp = new char*[rows+1]; for(int i = 0; i < rows; i++) temp[i] = arr[i]; delete []arr; arr = 0; arr = temp; rows += 1; return arr; } There are more than just `delete [] arr` issue. Something is really off about it.
24th Jun 2019, 2:34 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar