proc genArray(i, j: int) : seq[cstring] =
result = newSeqOfCap[cstring](j - i + 1)
for k in i .. j:
result.add(($k).cstring)
var a = genArray(1, 900)
for i in a:
echo i
The output contains mostly valid numbers, but some garbage. Of course cstrings exists only for C FFI, so using it this way is really stupid. I tried it just for a test of a robin hood hash, to investigate how key size degrades performance. (I was a bit surprised at first that hash is much slower for Nim strings as key than for plain ints -- but it is clear, int size is 4 or 8 byte, Nim string size should be at least twice, c pointer and size.) For the code above -- may GC just free the string immediately?
Yes, if the strings used are stored somewhere as string at the time they are checked, the output is normal, e.g.:
var s: seq[string]
proc genArray(i, j: int) : seq[cstring] =
result = newSeqOfCap[cstring](j - i + 1)
s = newSeqOfCap[string](j - i + 1)
for k in i .. j:
s.add $k
result.add((s[k-i]).cstring)
Thank you!
[EDIT]
OH, it is GC_ref()! Indeed trying gcRef() was my first idea, following by something like "import garbageCollector" as gcRef() was an unknown symbol. But I was already too tired for further investigations yesterday... At least it is not a hardware problem. :-)
or
proc genArray(i, j: int) : seq[cstring] =
result = newSeqOfCap[cstring](j - i + 1)
for k in i .. j:
let s = $k
GC_ref(s)
result.add(s.cstring)