I'm having a hard time understanding how to implem...
# kotlin-native
g
I'm having a hard time understanding how to implement `getline`'s sample in K/N: https://linux.die.net/man/3/getline (at the bottom)
Particularly, passing
&line
to
getline
without initializing it myself.
(I know the docs state I can initialize it myself, but I'd rather not do it and implement the example verbatim)
g
you need memScoped + alloc to allocate memory
o
Copy code
fun main() = memScoped {
    val inputBuffer = ByteArray(1024)
    val inputBufferPtr = alloc<CPointerVar<ByteVar>>()
    val inputBufferSizePtr = alloc<ULongVar>()

    val input = inputBuffer.usePinned {
        inputBufferPtr.value = it.addressOf(0)
        inputBufferSizePtr.value = inputBuffer.size.convert()
        val l = getline(inputBufferPtr.ptr, inputBufferSizePtr.ptr, stdin)
        if (l > 0)
            inputBuffer.stringFromUtf8(0, l.convert())
        else
            ""
    }
    println(input)

}
g
I see. I was hoping to be able to rely on getline to alloc the memory for me... But I understand why it has to be done this way. 🙂 Thanks!
o
nope, you can do both, just this way manages memory cleaner