Skip to content

Instantly share code, notes, and snippets.

@jacobsapps
Created April 30, 2025 21:33
Show Gist options
  • Save jacobsapps/9e472d0c1a03f993cac85e9e4ff96120 to your computer and use it in GitHub Desktop.
Save jacobsapps/9e472d0c1a03f993cac85e9e4ff96120 to your computer and use it in GitHub Desktop.
let size = MemoryLayout<Int64>.size // 8
let rawPointer = malloc(size)! // UnsafeMutableRawPointer
memset(rawPointer, 0, size)
for i in 0..<size {
rawPointer.assumingMemoryBound(to: UInt8.self)[i] = UInt8(i)
}
let buffer = UnsafeRawBufferPointer(start: rawPointer, count: size)
for byte in buffer {
print(byte) // 0, 1, 2, 3, 4, 5, 6, 7
}
free(rawPointer) // don't forget this!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment