Re: Creating C array

2019-03-29 Thread trtt
I'm not sure if I'm fit to improve the docs because I'm not good enough with 
nim's internal things and not good with english either. Maybe I'll try when I 
become better with either of them ;)


Re: Creating C array

2019-03-29 Thread cblake
You're welcome. Nim core devs are **_very_** willing to work with any and all 
comers on pull requests to improve documentation. Ignorance by otherwise 
reasonably patient and resourceful newcomers is **_not_** easy to simulate (in 
any project, really). In some ways it's a resource that decays with value as 
people learn their way around.

I agree that foreign function interface helper procs like those mentioned 
probably deserve a section somewhere, probably linked to from the area you 
mention. I mean, there is the `system.nim` module documentation, but it's kind 
of its own problem area.


Re: Creating C array

2019-03-29 Thread trtt
Thank you!

I wish the docs had mentioned this -

[https://nim-lang.org/docs/backends.html](https://nim-lang.org/docs/backends.html)


Re: Creating C array

2019-03-29 Thread cblake
Nim defines a 


proc allocCStringArray*(a: openArray[string]): cstringArray


Run

in the `system` module (no need to explicitly `import` it). There is also a 
corresponding: 


proc deallocCStringArray*(a: cstringArray)


Run

You probably want to use those two calls.


Re: Creating C array

2019-03-29 Thread trtt
Corrected:



proc kget*(elems: seq[string]): string =
  var arr: CArray[cstring]
  arr[0] = "start"
  var i = 1
  for elem in elems:
arr[i] = elem
i += 1
  var size: cint = cast[cint](elems.len() + 1)
  return $fn(size, addr arr)# tried it with unsafeAddr too


Run


Re: Creating C array

2019-03-29 Thread trtt
When I load the array manually, it works(somewhat):


var arr: CArray[cstring]
arr[0] = "start"
arr[1] = "arg1"
arr[2] = "arg2"
arr[3] = "arg3"
arr[4] = "arg4"
echo $fn(5, unsafeAddr arr)


Run

the C library works _correctly_ and the logs show good results while the nim 
program will output garbage.


Creating C array

2019-03-29 Thread trtt
Hi

> How to create and pass an array of string to nim and pass it to C?


type CArray{.unchecked.}[T] = array[0..0, T]#found it on the forum

proc fn(argc: cint, args: ptr CArray[cstring]): cstring {.importc, dynlib: 
"...".}


Run

I tried this but got *** stack smashing detected ***:  terminated:


proc fn*(elems: seq[string]): string =
  var arr: CArray[cstring]
  arr[0] = "start"
  var i = 1
  for elem in elems:
arr[i] = elem
i += 1
  var size: cint = cast[cint](elems.len() + 1)
  return $kget(size, addr arr)# tried it with unsafeAddr too


Run