I saw in some libraries and used the following technique to interop with C 
code. For example
    
    
    // sum.c
    long long sum(long long* xs, unsigned int len){
      long long s = 0;
      for(int i=0; i<len; i++) s += xs[i];
      return s;
    }
    
    
    Run
    
    
    # sum.nim
    {.compile: "sum.c" .}
    
    proc sum(buf: ptr clonglong, len: csize_t): clonglong {.importc.}
    
    proc sum_seq_wrapper(xs: seq[int]): int =
      if xs.len == 0: return 0
      let n = sum(cast[ptr clonglong](xs[0].unsafeAddr), xs.len.uint)
      result = n.int
    
    when isMainModule:
      let xs = @[1,2,3]
      echo sum_seq_wrapper(xs)
    
    
    Run

I am 100% sure, but it doesn't seem to me `xs` would be freed during `sum`. I 
also see people apply the same technique to string.

Reply via email to