Printing a Variable Memory Address in Swift

Printing a variable memory address in swift

Swift 2

This is now part of the standard library: unsafeAddressOf.

/// Return an UnsafePointer to the storage used for `object`.  There's
/// not much you can do with this other than use it to identify the
/// object

Swift 3

For Swift 3, use withUnsafePointer:

var str = "A String"
withUnsafePointer(to: &str) {
print(" str value \(str) has address: \($0)")
}

Swift - can only print object's memory address

You should override your class description property:

func description() -> String {
return "Business name: \(self.name), address: \(self.address), etc."
}

where you print all properties of YLPBusiness as you desire.

You can fix your problem mentioned in comments by turning your method into property:

public override var description: String {
return "Business name: \(self.name), address: \(self.address), etc."
}

It happened because Swift detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime.

Printing Object description at a specific memory adress

You can do that with that command line (accessing view property in this example :

expr -l objc -O -- [(UIViewController*)0x600003eb1f00 view]

You can find a similar question (and answer) here What's a proper way to print variable with a hex pointer given (Xcode 10.3, 11 betas)

How can I get the memory address of a value type or a custom struct in Swift?

According to Martin R' s answer

addressOf() cannot be used with struct variables. String is a struct, however, it is automatically bridged to NSString when passed to a function expecting an object.

According to nschum's answer, you can get the (stack) address of a struct, build-in type or object reference like this:

import UIKit


func address(o: UnsafePointer<Void>) -> Int {
return unsafeBitCast(o, Int.self)
}

func addressHeap<T: AnyObject>(o: T) -> Int {
return unsafeBitCast(o, Int.self)
}


struct myStruct {
var a: Int
}

class myClas {

}
//struct
var struct1 = myStruct(a: 5)
var struct2 = struct1
print(NSString(format: "%p", address(&struct1))) // -> "0x10f1fd430\n"
print(NSString(format: "%p", address(&struct2))) // -> "0x10f1fd438\n"

//String
var s = "A String"
var aa = s
print(NSString(format: "%p", address(&s))) // -> "0x10f43a430\n"
print(NSString(format: "%p", address(&aa))) // -> "0x10f43a448\n"

//Class
var class1 = myClas()
var class2 = class1
print(NSString(format: "%p", addressHeap(class1))) // -> 0x7fd5c8700970
print(NSString(format: "%p", addressHeap(class2))) // -> 0x7fd5c8700970

unsafeAddressOf(class1) //"UnsafePointer(0x7FD95AE272E0)"
unsafeAddressOf(class2) //"UnsafePointer(0x7FD95AE272E0)"

//Int
var num1 = 55
var num2 = num1
print(NSString(format: "%p", address(&num1))) // -> "0x10f1fd480\n"
print(NSString(format: "%p", address(&num2))) // -> "0x10f1fd488\n"

One thing I found is, if myStruct has no value, the address will be retain same:

struct myStruct {

}

var struct1 = myStruct()
var struct2 = struct1
print(NSString(format: "%p", address(&struct1))) // -> ""0xa000000000070252\n""
print(NSString(format: "%p", address(&struct2))) // -> ""0xa000000000070252\n""

Swift, Strings and Memory Addresses

func unsafeAddressOf(object: AnyObject) -> UnsafePointer<Void>

takes an AnyObject parameter, i.e. an instance of a class.
It returns the pointer to the storage used for the object referenced
by object.

addressOf() cannot be used with struct variables:

struct Foo { }
var f = Foo()
let a = unsafeAddressOf(f)
// error: cannot invoke 'unsafeAddressOf' with an argument list of type '(Foo)'

String is a struct, however, it is automatically bridged to NSString when passed to a function expecting an object. So

let word0 = "hello"
let p1 = unsafeAddressOf(word0)

actually executes

let p1 = unsafeAddressOf(word0 as NSString)

You get not the address of the word0 variable, but the pointer to the
memory location of the bridged NSString object.

It seems that you cannot make any assumptions on whether this bridging
returns the identical NSString object (or more generally, the same
Foundation object) when done repeatedly on the same Swift string. In a Playground, even

let word0 = "hello"
let p1 = unsafeAddressOf(word0)
let p2 = unsafeAddressOf(word0)
let p3 = unsafeAddressOf(word0)

returns three different addresses (but the same addresses in a compiled
project). The same observation (for arrays and dictionaries) was made
in A different bridging between Array and Dictionary.

lldb : Printing a variable's address

Yes, the -L location is telling you about the variable that lldb makes to represent the expression result, so that isn't what you want. Even though the common command alias print makes it seem like this command just prints values, it does a lot more than that: e.g. creating new entities in the running program. So the location of the expression result is not trivially related to the expression you evaluated.

Anyway, there are two easy ways to get at this. The first is to turn off the string summary, so you can see the actual result of printing the address:

(lldb) expr --raw -- &my_string
(string *) $14 = 0x00007fff5fbff618

Another way to get at the same data is to use the "frame variable" command. This command gives you access to local variables, without the overhead of the full expression parser. Since frame variable prints the variables directly as reported by the debug info, in that case the -L option is exactly the variable's location:

(lldb) frame var -L my_string
0x00007fff5fbff618: (std::__1::string) my_string = "Some string here"


Related Topics



Leave a reply



Submit