New to the Nim language, I'm trying to store the address of an object in a 
table to interface it with C code. I've realized that storing the entire object 
requires a lot of memory, especially if I'm storing a lot of objects. So 
instead of storing the object, I want to store its address.
    
    
    type
      myObj = ref object
        age: int
    
    # Init global table
    var testTable  = initTable[string, ptr myObj]()
    
    proc initTableObj (): cint =
      # Constructor
      let obj = newObj()
      obj.age = 20
      
      # Store address in 'testTable'
      testTable["key1"] = obj.addr
      return 0
    
    proc callTableObj (): cint =
      let p = testTable["key1"]
      
      if p == nil:
        echo "yes"
        return 1
      # Deference
      let newObj = p[]
      
      if newObj == nil:
        echo "yes"
        return 1
      else:
        # Error here : SIGSEGV: Illegal storage access. (Attempt to read from 
nil?)
        echo "Age: ", newObj.age
      
      return 0
    
    
    Run

I get this error **SIGSEGV: Illegal storage access** I'd like to know what I'm 
doing wrong, If I call the procedure `initTableObj` inside my `callTableObj` 
procedure, everything works.

Reply via email to