I'm stuck for a few days on a bug and I need assistance of a pair programmer because my eyes are blind where the bug hides.
The code is for a sudoku solver using Knuth's dancing links algorithm. I create a 25x25 sudoku board (initBoard), convert it to a 15626x2500 cover matrix of the boolean constraints (convertInCoverMatrix in the solve procedure), and then prepare Knuth's Dancing Links data structure (initDLX that calls createDLXList).
The program stops with a segmentation fault when entering createDLXList.
Though the data structures used can be large, the cover matrix is a list of ref nodes allocated on the heap, and I don't think that I'm squashing the 8 MB program stack on linux. I've tried printing stack usage when entering these procs and there's no flagrant stack use.
This program does use only safe nim features and I don't understand why it crashes that way.
Is there someone with fresh eyes that could point me at what I'm doing wrong?
I think I know the reason you're program is crashing.
As @dawkot already mentioned 246 is suspicious. This issue is relevant. Currently an array is always copied onto the stack when looping over it, which can be catastrophal. Inserting echo sizeof(CoverMatrix) explains the whole thing: CoverMatrix is 37 MB big, which floods the stack.
With a small modification it works: https://play.nim-lang.org/#ix=26fC
Perhaps the reason of your crash is because the CoverMatrix array is quite large, being 15625x2500 boolean (8 bits), so at least 39 MB. Using a CoverMatrix variable directly is larger than the program stack. That's the reason I'm using CoverMatrixRef to have the cover matrix allocated in the heap.
That's strange that the first block runs...
THANK YOU!
I was mentioned this bug before but I did not understood its impact... This type of bug is quite frustrating. I'm going to check the rest of my code if it does not occur elsewhere.