@@ -126,28 +126,21 @@ val originalPtr = longValue.toCPointer<T>()
126126The native memory can be allocated using the ` NativePlacement ` interface, for example:
127127
128128``` kotlin
129+ @file:OptIn(ExperimentalForeignApi ::class )
129130import kotlinx.cinterop.*
130131
131- @OptIn( ExperimentalForeignApi :: class )
132+ val placement : NativePlacement = // See below for placement examples
132133val byteVar = placement.alloc<ByteVar >()
133- ```
134-
135- Or:
136-
137- ``` kotlin
138- import kotlinx.cinterop.*
139-
140- @OptIn(ExperimentalForeignApi ::class )
141134val bytePtr = placement.allocArray<ByteVar >(5 )
142135```
143136
144137The most logical placement is in the object ` nativeHeap ` . It corresponds to allocating native memory with ` malloc ` and
145138provides an additional ` .free() ` operation to free allocated memory:
146139
147140``` kotlin
141+ @file:OptIn(ExperimentalForeignApi ::class )
148142import kotlinx.cinterop.*
149143
150- @OptIn(kotlinx.cinterop.ExperimentalForeignApi ::class )
151144fun main () {
152145 val size: Long = 0
153146 val buffer = nativeHeap.allocArray<ByteVar >(size)
@@ -165,10 +158,10 @@ automatically freed after leaving the scope.
165158For example, a C function returning values through pointer parameters can be used like:
166159
167160``` kotlin
161+ @file:OptIn(ExperimentalForeignApi ::class )
168162import kotlinx.cinterop.*
169163import platform.posix.*
170164
171- @OptIn(ExperimentalForeignApi ::class )
172165val fileSize = memScoped {
173166 val statBuf = alloc< stat> ()
174167 val error = stat(" /" , statBuf.ptr)
0 commit comments