0

I'm writing a simple sliding tiles (3x3) solver. It's not the best approach for sure i just generate all the possible configuration but I don't know why during the execution my pc freeze and I must manuallly restart. the's the main loop

while(!(tree->s==final))
{ 
    //copy it in tree   

    expand_node(tree);
    //check if in open then add if not
    it++;
}      
print_s(tree->s);

PS: I compiled everything with

g++ -Wall -Wextra -std=c++11 main.cpp
  • 6
    Please include the relevant code in the body of your question. Those links will rot and the question won't make sense in the future for any users who come to it. Plus, the pastebin ads are huge and needlessly distracting. – alter_igel Oct 15 '18 at 20:06
  • 4
    Provide a [mcve]. ~400 LOC linked on pastebin are definitely not a [mcve]. – Swordfish Oct 15 '18 at 20:08

1 Answers1

1

Inside your range based for loops you are adding to the list:

for(auto v : open)
    if(!(v.s==tree->childs[i].s))
        open.push_back(tree->childs[i]);

This will cause the list to grow and trigger another iteration until all the memory on your machine is exhausted. Your machine probably doesn't freeze just gets extremely slow whilst your OS is swapping memory to disk.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • My idea was to generate all the possible configuration until I find the solution.. Is this approach not right? What could be a better approach? –  Oct 15 '18 at 20:23
  • @dario Without a [mcve] its difficult to tell you'f have to debug your code yourself but it looks to me likely to end in an infinite loop – Alan Birtles Oct 15 '18 at 20:27
  • This question says adding items to open while iterating is Undefined Behavior: https://stackoverflow.com/questions/35468207/is-it-legal-to-add-elements-to-a-preallocated-vector-in-a-range-based-for-loop-o – drescherjm Oct 15 '18 at 20:49
  • @drescherjm that only applies to vector, the behaviour is defined with `std::list` although it changes depending which c++ standard you are using https://en.cppreference.com/w/cpp/language/range-for. c++17 would fix the infinite loop – Alan Birtles Oct 16 '18 at 06:15