Hey! Is it possible to create an `CPointer<Bool...
# kotlin-native
s
Hey! Is it possible to create an
CPointer<BooleanVar>
without the use of the nativeHeap? I would like to create one on the stack, but everything i've tried failed. Currently im using this
Copy code
val flag = nativeHeap.alloc<BooleanVar>(); flag.usePinned { it.get().ptr /* <- CPointer<BooleanVar> */}
I need to call
fun fileExistsAtPath(path: String, isDirectory: CPointer<BooleanVar?): Boolean
(iOS) and want to know if
path
is a directory or not.
j
Try
cValue<T>(){...}
function to allocate a Var. The
memScope{..}
block is a replacement for
nativeHeap
, it releases all allocations at the end of the block
s
But that would still use the heap, right? Something like this is not possible?
Copy code
bool b = false; bool *ptr = &b; functionThatUsesBoolPtr(ptr);
j
I think we cannot extract a mutable pointer for a variable. I would implemented that as follows:
Copy code
import kotlinx.cinterop.BooleanVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value

fun x(b: CPointer<BooleanVar>?) = 42

val test = memScoped {
  x(alloc<BooleanVar> { value = false }.ptr)
}
where
alloc
is the function that is available inside the
memScoped
block. It allocates the boolean
s
alright, thank you very much!