If you want to pass a pointer, you can use ```memS...
# kotlin-native
d
If you want to pass a pointer, you can use
Copy code
memScope {
    val size = alloc<UIntVar>()
    sysctlbyname(..., ..., size.ptr,)
    size.value
}
n
Thank you very much, I will now try this out!
j
beware, it will release the memory after the
memScope
block is completed
d
Remember to import kotlin.native.cinterop.*
j
use
Arena
or
GlobalScope
for a loner-living things
n
So would this be the correct implementation?
Copy code
memScoped {
                val size = alloc<ULongVar>()
                sysctlbyname("hw.machine", null, size.ptr, null, 0)
                val machine = allocArray<ByteVar>(size.value.toLong())
                sysctlbyname("hw.machine", machine, size.ptr, null, 0)
                return machine.getPointer(this).toKString()
            }
d
I don't know what the sys function does. You have to make sure the data in machine is somehow copied to return it
You can replace getPointer(this) with ptr
n
This is the corresponding Objective C code that I’m trying to translate:
Copy code
size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);
    return platform;
You can replace getPointer(this) with ptr
This doesn’t work for
CArrayPointer
for some reason.
d
Well, machine is freed when you leave memScope so you have to use it before that
n
Yeah, I make a Kotlin out of it, so should be fine?
d
Oh yeah, that should be fine
n
Cool, thank you very much! Very little documentation, if not this channel I would give up long time ago.
d
You're welcome