What's the analogue in Kotlin cinterop of the unar...
# kotlin-native
m
What's the analogue in Kotlin cinterop of the unary & operation in C which gets the address of the data? For example, in C if a process has been forked the parent process waits for the child process to finish by using waitpid
Copy code
int status = 0;
waitpid(pid, &status, WCONTINUED | WTRUNCATED);
Kotlin has access to waitpid via the platform.posix library. However, in Kotlin, neither
Copy code
var status:Int = 0;
waitpid(pid, status, WCONTINUED or WTRUNCATED);
nor
Copy code
var status:Int = 0;
waitpid(pid, status.ptr, WCONTINUED or WTRUNCATED);
works.
the problem is that status is an integer while ptr is only available for CVariable types. So I need to convert status into a CVariable
Actually since status is only passed by reference in C functions one can directly define status as a CVariable in the first place
Copy code
memScoped {
    var status:IntVar = alloc<IntVar>();
    waitpid(pid, status.ptr, WCONTINUED or WTRUNCATED);
}
l
If you can define it using a citerop type from the start, that's ideal, but in case you run into a future case where you can't, you'll need to pin the object to get the address. The gc is technically allowed to move an object at any time, so you can call 'pin' on the object to stop the GC from moving it, and get the address from the pinned reference. Don't unpin until C no longer needs the pointer. There's a usePinned method that takes a lambda, but it's important the pointer never leaves that lambda.