Advertisement
Guest User

memory address of empty swift struct

a guest
Sep 12th, 2017
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.61 KB | None | 0 0
  1. // https://stackoverflow.com/a/45777692/5536516
  2.  
  3. import Foundation
  4.  
  5. func memoryAddress(of structPointer: UnsafeMutableRawPointer) -> String {
  6.     return structPointer.debugDescription
  7. }
  8.  
  9. struct S {/* let foo = 1 */}
  10. var s = S()
  11.  
  12. struct T {/* let bar = 1 */}
  13. var t = T()
  14.  
  15. let m = memoryAddress(of: &s)
  16. let n = memoryAddress(of: &s)
  17. let o = memoryAddress(of: &t)
  18.  
  19. // different addresses
  20. print("m = \(m)")
  21. print("n = \(n)")
  22. print("o = \(o)")
  23.  
  24. // same addresses
  25. print("directly = \(memoryAddress(of: &s))")
  26. print("directly = \(memoryAddress(of: &s))")
  27. print("directly = \(memoryAddress(of: &t))")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement