I want to make a list of string pointers within a ...
# kotlin-native
a
I want to make a list of string pointers within a NativePlacement, but
createValues()
is creating an CValues with size 24 instead of 3. Is there a way to create CValues with a specific size?
Copy code
import kotlinx.cinterop.*

fun main() {
  nativeHeap.cValuesOf(listOf("1", "2", "3"))
}

typealias StringPointer = CPointerVarOf<CPointer<ByteVar>>

fun NativePlacement.cValuesOf(strings: List<String>): CValues<StringPointer> {
  println("strings.size ${strings.size}")
  val array = createValues<StringPointer>(strings.size) { i ->
    val str = strings[i].cstr
    val arr = allocArray<ByteVar>(str.size)
    str.place(arr)
  }
  println("strings array.size ${array.size}")
  return array
}
prints:
Copy code
strings.size 3
strings array.size 24
o
If you will follow the long chain of functions, you will see, that when creating it via
createValues
,
array.size
returns amount of bytes in
CValues
- in this case you create
CValues
where there are 3 pointers (each of 8 bytes) What do you need this for?
Also, BTW, you are not using receiver of
createValues
at all, so probably in the end it will be anyway empty ^_^
a
I need to pass a list of strings into this C function:
Copy code
int ListView(const char **text, int count);
the generated Kotlin binding is
Copy code
public expect fun GuiListViewEx(text: CValuesRef<CPointerVar<ByteVar>>?, count: Int): Int { /* compiled code */ }
you are not using receiver of
Ahh yes, good spot. I assumed it would be a builder like
List(5) { i -> ...}
o
most likely this is what you are looking for: https://github.com/whyoleg/ffi-kotlin/blob/2c78adb60984f38f10db0cccd5a3e01d4f8f575[…]ng/cx/compiler/src/nativeMain/kotlin/clang/CXTranslationUnit.kt simply
allocArray(strings.size) { value = strings[it].cstr.ptr }
most likely last
ptr
need to be replaced with
.getPointer(this)
because you need
NativePlacement
but
.ptr
declared in
MemScope
a
allocArray()
returns
CArrayPointer<>
, but I need
CValues<>
oh, no? I've confused myself
o
You need CValuesRef - CPointer extends it
a
yup, that's got it working! Thanks Oleg
👍 1