iosnsobject

Get memory address of Objective C object?


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

NSLog(@"%p", anObject);

I would rather get the plain long value.


Solution

  • 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);