I have to call an external API using C arrays from Nim `seq[]`. The API is 
using the array content for the duration of the call. What would be the safest 
and future proof way of doing it?

**1\. Share seq[] private array for the duration of the call**
    
    
    var s: seq[Obj] # contains the data that must be sent to the external API
    api_call(s.len.cuint, addr(s[0]))
    
    
    Run

**2\. Manage a temporary unchecked array**
    
    
    var s: seq[Obj]
    var
      arrPtr = alloc(s.len * sizeof(Obj))
      arr = cast[ptr UncheckedArray[Obj]](arrPtr)
    
    for i, v in s:
        arr[i] = v
    
    let a = api_call(s.len.cuint, arr)
    
    dealloc arrPtr
    
    
    Run

I don't care about performance here but the more future proof and safest code. 
Version #1 is shorter to write and less prone to forgetting deallocating the 
memory of version #2. But it seems to me that with version #2, at least I don't 
depend on how memory is used by seq[].

What would you recommend?

Reply via email to