How do I properly make this code C++ in Kotlin/Nat...
# kotlin-native
t
How do I properly make this code C++ in Kotlin/Native?
Copy code
long ns;
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
ns = spec.tv_nsec;
I came up with this Kotlin/Native syntax but I can't access the tv_nsec member of the timespec struct and this is the only working code so far:
Copy code
val spec: CValuesRef<timespec>? = null
clock_gettime(CLOCK_REALTIME, spec)
to actually access the output, I would need the following code (right)?:
Copy code
var ns: Long
val spec: CValuesRef<timespec>? = null
clock_gettime(CLOCK_REALTIME, spec)
ns = spec.tv_nsec
So, how do I correctly do this?
m
Don't have all the details in mind right now but look into
memScoped
: https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/mem-scoped.html
I'm leaning towards something like:
Copy code
memScoped {
  val spec = alloc<timespec>()
  clock_gettime(CLOCK_REALTIME, spec.ptr)
}
Don't take this as gospel though as it's been a while I haven't tried this
t
Thanks a lot! It works!
🎉 1