Note that there are some explanatory texts on larger screens.

plurals
  1. POWord search algorithm segmentation fault
    primarykey
    data
    text
    <p>i've been working on word search algorithm for quite long I think i made it good and decided to test limits. I've created program which makes file as big as I want to. So i made a matrix 10000 * 10000 (10000000 letters) and really long word from top left corner to bottom right corner. Thing is that it works with 4000 * 4000 matrix but then it gets bigger it just crashes. I tried to comment all other checkings for possible location and left the right one and it works perfectly even with 10000 * 10000 matrix but as soon as I add other checks it stops and I have no idea why. Any suggestions?</p> <p><strong>My code:</strong></p> <pre><code> #include &lt;iostream&gt; //Might Be: #include &lt;string&gt; // &lt;-----&gt; #include &lt;fstream&gt; // /-\ (1)/\ /\(3) #include &lt;new&gt; // | \ / #include &lt;cstdlib&gt; // | \ / // | \ / // | \ / // | \ / // \_/ (2)\/ \/(4) // using namespace std; //Loop[4] //Loop[5] int * Possibles(int Widht, int Height, int Poz, int Poz1, int Leng, int * Possible) { if(Poz1 &lt; Widht - Leng + 1) // To right { Possible[0] = 1; } if(Poz1 &gt;= Leng - 1) // To left { Possible[1] = 1; } if(Poz &lt;= Height - Leng) // From top to bottom { Possible[2] = 1; } if(Poz &gt;= Leng) // From bottom to top { Possible[3] = 1; } if(Poz + Leng &lt;= Height &amp;&amp; Poz1 + Leng &lt;= Widht) //(2) { Possible[4] = 1; } if(Poz + Leng &lt;= Height &amp;&amp; Poz1 - Leng + 1 &gt;= 0) //(4) { Possible[5] = 1; } if(Poz - Leng + 1 &gt;= 0 &amp;&amp; Poz1 - Leng + 1 &gt;= 0) //(1) { Possible[6] = 1; } if(Poz - Leng + 1 &gt;= 0 &amp;&amp; Poz1 + Leng &lt;= Widht) //(3) { Possible[7] = 1; } return Possible; } int * Zero(int * Possible) { Possible[0] = 0; Possible[1] = 0; Possible[2] = 0; Possible[3] = 0; Possible[4] = 0; Possible[5] = 0; Possible[6] = 0; Possible[7] = 0; return Possible; } string Next(string * NewMatrix, int Height, int Widht) { return NewMatrix[Height].substr(Widht, 1); } bool Find(string Word, int Poz, int Poz1, int Look, string Have, string * Matrix, int * Possible, int Backup, int Backup1) { if(Have == Word) { return true; return Possible; } string NewLet = Word.substr(Look, 1); if(Possible[0] == 1) { if(NewLet == Next(Matrix, Poz, Poz1 + 1)) { Have += NewLet; return Find(Word, Poz, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[0] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[1] == 1) { if(NewLet == Next(Matrix, Poz, Poz1 - 1)) { Have += NewLet; return Find(Word, Poz, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[1] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[2] == 1) { if(NewLet == Next(Matrix, Poz + 1, Poz1)) { Have += NewLet; return Find(Word, Poz + 1, Poz1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[2] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[3] == 1) { if(NewLet == Next(Matrix, Poz - 1, Poz1)) { Have += NewLet; return Find(Word, Poz - 1, Poz1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[3] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[4] == 1) { if(NewLet == Next(Matrix, Poz + 1, Poz1 + 1)) { Have += NewLet; return Find(Word, Poz + 1, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[4] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[5] == 1) { if(NewLet == Next(Matrix, Poz + 1, Poz1 - 1)) { Have += NewLet; return Find(Word, Poz + 1, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[5] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[6] == 1) { if(NewLet == Next(Matrix, Poz - 1, Poz1 - 1)) { Have += NewLet; return Find(Word, Poz - 1, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[6] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } if(Possible[7] == 1) { if(NewLet == Next(Matrix, Poz - 1, Poz1 + 1)) { Have += NewLet; return Find(Word, Poz - 1, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1); } else { Possible[7] = 0; Have = Word.substr(0, 1); return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1); } } return false; } string Diro(int * Possible) { string Dir; bool Next = true; if(Possible[0] == 1 &amp;&amp; Next == true) { Dir = " From right to left"; Next = false; } if(Possible[1] == 1 &amp;&amp; Next == true) { Dir = " From left to right"; Next = false; } if(Possible[2] == 1 &amp;&amp; Next == true) { Dir = " From top to bottom"; Next = false; } if(Possible[3] == 1 &amp;&amp; Next == true) { Dir = " From bottom to top"; Next = false; } if(Possible[4] == 1 &amp;&amp; Next == true) { Dir = " "; Next = false; } if(Possible[5] == 1 &amp;&amp; Next == true) { Dir = " "; Next = false; } if(Possible[6] == 1 &amp;&amp; Next == true) { Dir = " "; Next = false; } if(Possible[7] == 1 &amp;&amp; Next == true) { Dir = " "; Next = false; } return Dir; } int main() { int Height = 0, Widht = 0, Numb = 0; int Loop[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; int * Possible = new int[8]; string Dir, Search, Tempo, Temp; ifstream Data("C:/Users/Magician/AppData/Local/VirtualStore/Program Files (x86)/CodeBlocks/MakeMaze/Files/Maze.txt"); Data &gt;&gt; Widht &gt;&gt; Height; string * NewMatrix = new string[Height]; while(Loop[7] &lt; Height) { Tempo = ""; Loop[8] = 0; while(Loop[8] &lt; Widht) { Data &gt;&gt; Temp; Tempo += Temp; Loop[8]++; } NewMatrix[Loop[7]] = Tempo; Loop[7]++; } Data &gt;&gt; Numb; string * Words = new string[Numb]; while(Loop[2] &lt; Numb) { Data &gt;&gt; Words[Loop[2]]; Loop[2]++; } Data.close(); while(Loop[3] &lt; Numb) { Search = Words[Loop[3]].substr(0, 1); Loop[4] = 0; while(Loop[4] &lt; Height) { Loop[5] = 0; while(Loop[5] &lt; Widht) { if(NewMatrix[Loop[4]].substr(Loop[5], 1) == Search) { Zero(Possible); Possibles(Widht, Height, Loop[4], Loop[5], Words[Loop[3]].size(), Possible); if(Find(Words[Loop[3]], Loop[4], Loop[5], 1, Search, NewMatrix, Possible, Loop[4], Loop[5])) { cout &lt;&lt; Words[Loop[3]] &lt;&lt; " At: " &lt;&lt; Loop[4] + 1 &lt;&lt; " collumn, symbol " &lt;&lt; Loop[5] + 1 &lt;&lt; " " &lt;&lt; Diro(Possible) &lt;&lt; endl; Loop[5] = Widht; Loop[4] = Height; } } Loop[5]++; } Loop[4]++; } Loop[3]++; } delete [] Possible; delete [] Words; delete [] NewMatrix; return 0; } </code></pre> <p>If you didn't understood what I wrote before: when I comment every if(Possible[] == ) except for if(Possible[5] == 1) in function Find() algorithm works then all allowed it doesn't. I've tried with 100 * 100 matrix with a lot of words to find and everything's ok.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload