Objective-C>捕获内存中的对象地址

时间:2013-07-11 08:32:31

标签: objective-c memory-management memory-address

如何在代码中的某个位置捕获对象的地址? 例如:

NSString * aString = @"bla bla";
//what is the current address of aString. i.e to which address in memory does it currently point to
aString = @"la la la";
//what is the current address of aString.

2 个答案:

答案 0 :(得分:6)

示例:

NSString *temp = @"123";

uintptr_t ptrAddress = (uintptr_t) temp;

NSLog(@"%@ %p %lu", temp, temp, ptrAddress);

控制台:

  

2013-07-11 11:51:20.796 asd [6474:907] 123 0x17985c 1546332

它可能对您有用 - NSPointerArray(iOS 6 +)

答案 1 :(得分:4)

这很简单,只需这样做:

NSLog(@"%p", aString);

这是打印指针的格式规范。