1

Say we are working in Objective-C

    NSDate *now = [NSDate date];
    NSLog(@"The new date lives at %p", now);
    NSLog(@"The new date lives also at %p", &now);

The NSLogs will spit out different memory addresses and that is confusing me. One of the NSLogs is telling me the address of the object now, but which one? Is one giving the address of the pointer and the other giving the address of the object?

Thank you

2 Answers 2

2

You are right. The first log message is showing you the address of the NSDate object itself, which should be in dynamic memory somewhere ("on the heap"). The second line is the address of your pointer variable 'now', which in this case is a local variable and should be on the stack.

So you have a local variable on the stack called now. now is a pointer, with the value of that now variable being the memory address of your NSDate.

Sign up to request clarification or add additional context in comments.

Comments

0

it's the difference between pointer and pointer's pointer.

2 Comments

No, it's the difference between an address and the address of that address.
:),that's my mean,just let him to think in the same way.