0

Is there any better way to get the memory address than this?

NSLog(@"%p", anObject);

I would rather get the plain long value.

Mick
  • 954
  • 7
  • 17

1 Answers1

1

Also you can caste some Type* to intptr_t, and look at this address in decimal representation:

NSLog(@"%lu", (uintptr_t)anObject);

To represent pointer address as integer in C exists 2 types: intptr_t and uintptr_t.

intptr_t is defined as __darwin_intptr_t.
and __darwin_intptr_t defined as long:

typedef long            __darwin_intptr_t;

uintptr_t defined as unsigned long:

typedef unsigned long           uintptr_t;

I think what for uintptr_t I should use %lu and for intptr_t I should use %li:

NSLog(@"%lu", (uintptr_t)anObject);
NSLog(@"%li", (intptr_t)anObject);
Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40
  • would you like to change your answer to: NSLog(@"%lu", (intptr_t) anObject); ? I just tested that successfully. %d produced a negative number with too few bits. – Mick Dec 17 '20 at 21:34
  • Also I think, that uintptr_t will be better type for this. – Andrew Romanov Dec 18 '20 at 02:46