Hi,

I was struggling in solving an exercise on adventofcode, filling a Nim array 
with complex structures. I had a segmentation fault, and I wanted to check my 
code (especially the instantiation of nested arrays) using a simple test code.

This is the code:
    
    
    const
      H = 1000
      W = 1000
    
    type myArray = array[H, array[W, int]]
    
    proc fillArray(): myArray =
      echo "Instanciating..."
      var a: myArray
      echo "OK!"
      for h in 0..H:
        for w in 0..W:
          a[h][w] = 1
      result = a
    
    proc main() =
      echo "Calling fillArray procedure..."
      let a : myArray = fillArray()
    
    when isMainModule:
      main()
    
    
    Run

According to the Nim documentation (I don't remember if it was on Nim by 
Example, or in the Nim manual), but var a: myArray instanciates by itself the 
nested array. This code produces also or a segmentation fault error:
    
    
    Calling fillArray procedure...                                              
                                                  │~
    [1]    15746 segmentation fault (core dumped)  ./nim_test
    
    
    Run

According to this output, "something" happened at the fillArray procedure call 
(as the first echo statement has not been executed... I think). I will try to 
debug the code using gdb or something, but I would be grateful if someone has a 
clear explanation/documentation to provide me, in order to explain the 
behaviour (and the mistake I did).

Btw, I use the last version of Nim: 0.19.0.

Thanks in advance, and best wishes for the end of this year.

Reply via email to