I don't think you can ? My current mental model is that
CPointer
is for something that lives on the heap (has reference semantics) while
CValues
has value semantics (might be a const string or something else, not sure)
mbonnin
10/10/2021, 9:18 PM
You can alloc a
CPointer
and memcpy to it though:
Copy code
val src = "Hello".cstr
val dst = allocArray<ByteVar>(src.size)
memcpy(dst, src, src.size.convert())
(don't take this as gospel, it might very well be wrong about size, terminating zero and all that kind of thingies)
d
Dominaezzz
10/10/2021, 10:13 PM
memScoped { yourFunction("The string".cstr.ptr) }
. Haven't done native in a while but this is a shortcut iirc.
👀 1
m
martmists
10/10/2021, 11:10 PM
@Dominaezzz that one won't work since I need to return it, but why would I use that over, say, a StableRef (which I found as alternative)
❓ 1
d
Dominaezzz
10/11/2021, 6:26 AM
Stable ref? How does that help you?
m
martmists
10/11/2021, 10:14 AM
It allows me to get a pointer from a variable rather than having to allocate the string again and copy it into there which would lead to a memory leak
d
Dominaezzz
10/11/2021, 10:31 AM
The pointer you get from stable ref is opaque. It doesn't point to any data you're allowed to touch. It's meant to be used for callbacks, like a C handle for Kotlin objects. Also they need to be released so you'll leak either way.