Skip to content

Instantly share code, notes, and snippets.

@sbooth
Created November 3, 2016 12:06
Show Gist options
  • Save sbooth/ee86d52e03a5cfa42d725dbcb183a465 to your computer and use it in GitHub Desktop.
Save sbooth/ee86d52e03a5cfa42d725dbcb183a465 to your computer and use it in GitHub Desktop.
Test the behavior of UnsafeMutablePointer.initialize(to:)
import Foundation
class Test {
init() {
print("init")
}
deinit {
print("deinit")
}
}
func make_ptr(_ test: Test) -> UnsafeMutablePointer<Test> {
let test_ptr = UnsafeMutablePointer<Test>.allocate(capacity: 1)
test_ptr.initialize(to: test)
return test_ptr
}
func delete_ptr(_ test_ptr: UnsafeMutablePointer<Test>) {
test_ptr.deinitialize()
test_ptr.deallocate(capacity: 1)
}
let test_ptr = make_ptr(Test())
delete_ptr(test_ptr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment