@OptIn(ExperimentalForeignApi::class) fun memScopedFileWriteExample() { memScoped { val path = "/tmp/memscoped_example.txt" val content = "Hello from Kotlin/Native!" val fd = open(path, O_WRONLY or O_CREAT or O_TRUNC, (S_IRUSR or S_IWUSR).toUInt()) if (fd < 0) { perror("open") return } val cContent = content.cstr.getPointer(this) val written = write(fd, cContent, content.length.toULong()) if (written < 0) { // 後略 例えば Kotlin の String を C には そのまま渡せない C とやり取りする際に⼀時的なネイティブ メモリ領域を確保したいときに使う このブロックを抜けた時点で、確保した メモリは解放される