Search code examples
scalascala-native

Scala Native - How to convert Int to Ptr[CInt]?


What's the right way to convert VM's Int to unmanaged CInt pointer from scala.scalanative.unsafe?

val num: Int = 5
val nativeInt: Ptr[CInt] = // ?

Solution

  • This seems to be working for me:

    def toCIntPtr(num: Int)(using z: Zone): Ptr[CInt] = {
        val nativeInt: Ptr[CInt] = alloc[CInt](sizeof[CInt])
        !nativeInt = num
        nativeInt
    }
    
    // implicit zone present
    val num: Int = 5
    val nativeInt: Ptr[CInt] = toCIntPtr(num)