What is the recommended way to get data out of a C...
# kotlin-native
t
What is the recommended way to get data out of a CArrayPointer after it has been passed to a C library and filled with data? This snippet works, but feels wrong.
Copy code
val opusOutData = allocArray<opus_int16Var>(outputLen)
// pass CArrayPointer to C library to be filled
...
// Pull the new data out of the CArrayPointer to be used?
return (0 until outputLen).map { opusOutData.get(it) }.toTypedArray()
j
You can just pass a pointer to the
ByteArray
itself to fill in C. Then you don't need to copy the memory.
Copy code
val byteArray = ByteArray(outputLen * sizeOf<opus_int16Var>())
byteArray.usePinned {
    // pass it.addressOf(0).reinterpret<opus_int16Var>() to C library to be filled
}
return byteArray
t
Thanks for the advice Jeff! I think that is much cleaner.
👍🏼 1