Hello @phoenix27,

Sorry to be straightforward, but you are doing all wrong ! ;-)

If I'm not mistaken, the seq object looks like this 
(<https://github.com/nim-lang/Nim/blob/646bd99d461469f08e656f92ae278d6695b35778/lib/system/seqs_v2.nim#L20>)
 :
    
    
    type
      NimSeqPayload[T] = object
        cap: int
        data: UncheckedArray[T]
      
      NimSeqV2*[T] = object # \
        # if you change this implementation, also change seqs_v2_reimpl.nim!
        len: int
        p: ptr NimSeqPayload[T]
    
    
    Run

So if you cast a seq to a pointer, you will not get at all the buffer ! In 
fact, you get a pointer that points to something totally different (and you 
risk a SIGESEGV)

The prefered way to use a seq with c is :

  * Initialize it with a length `var myList = newSeq[T](1024)`
  * Get the address of the underlying buffer `var bufAddr = addr(myList[0])`
  * Pass it to a c function along with your size (to avoid index out of bound)



You can also use an array. Contrary to a seq, it doesn't encapsulate a buffer:
    
    
    var arr: array[64, int]
    doAssert(addr arr == addr arr[0])
    
    var list = newSeq[int](64)
    doAssert(addr list != addr list[0])
    
    
    Run

If you want to allocate, or grow/shrink a hap buffer in c, you won't be able to 
use seq effiently or easily (Unfortunatly, seq API don't provide a way to 
transform a heap buffer into a seq without copy. ).

Reply via email to