Nim has implicit initialization ( 
[http://nim-lang.org/docs/manual.html#statements-and-expressions-var-statement](http://forum.nim-lang.org///nim-lang.org/docs/manual.html#statements-and-expressions-var-statement)
 ) so: 
    
    
    var x : array[10,char]
    echo repr x # ['\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0']
    

Anyway, `alloc0` and the like return pointers, so you need to cast to `ptr 
array[]` :
    
    
    var inputs = cast[ptr array[10,int]](alloc0(sizeof(int) * 10))
    inputs[0] = 1
    inputs[1] = 2
    inputs[2] = 3
    for i in 0..9:
      echo inputs[i]
    echo repr inputs
    
    var test = alloc0(10)
    zeroMem(test, 10)
    echo repr cast[ptr array[10,char]](test)
    

Produces
    
    
    1
    2
    3
    0
    0
    0
    0
    0
    0
    0
    ref 000000000018F050 --> [1, 2, 3, 0, 0, 0, 0, 0, 0, 0]
    ref 0000000000191050 --> ['\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', 
'\0', '\0']
    

Or just use create: 
    
    
    var ar = create array[10,char]
    echo repr ar # ref 000000000092F050 --> ['\0', '\0', '\0', '\0', '\0', 
'\0', '\0', '\0', '\0', '\0']
    

Note that I don't think nim's garbage collector keeps track of pointers you get 
from `alloc` etc, so you may have to deallocate them yourself to avoid memory 
leaks

Reply via email to