Thanks, that's very helpful. When I explicitly allocate in Nim and then free
from C then my code fails with "pointer being freed was not allocated". But
handling the memory entirely from C and making it available to Nim via an
argument works fine for me (that is, have Nim copy its output to C-provided
memory instead of returning its output):
void gimme_wrapper(char* output, int maxlength) {
NimMain();
gimme(output, maxlength);
}
Run
proc gimme(output: cstring, maxlength: int) {.exportc.} =
let tmp = "Hey there C code! " & $rand(100)
copyMem(output, tmp.cstring(), tmp.len + 1) # TODO: truncate tmp to
maxlength
Run