win10, nim0.14.2
# use alloc0
var inputs = cast[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[array[10,char]](test)
1
2
3
6553000
0
6552640
6553000
1
7607136
1
[1, 2, 3, 6553000, 0, 6552640, 6553000, 1, 7607136, 1]
['P', '@', '\25', '\0', '\0', '\0', '\0', '\0', '\9', '\0']
Maybe I use the incorrect way?
And array can't convert to pointer, How to init all val 0 if I use array[10000,int] var repeated. (dont use 'for')
Nim has implicit initialization ( http://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