Re: pointers in objective-c?
Re: pointers in objective-c?
- Subject: Re: pointers in objective-c?
- From: "Michael Ash" <email@hidden>
- Date: Fri, 1 Aug 2008 16:47:11 -0400
On Fri, Aug 1, 2008 at 2:43 PM, Wayne Shao <email@hidden> wrote:
> It seems that every object is a pointer in the sample code I have
> seen. Is there any distinction between an object and its pointer?
Others have addressed the general concepts involved, but I wanted to
answer this question specifically.
There is a distinction between an object and its pointer, an important
distinction. An object is a blob of memory with a certain layout. That
layout is determined by the instance variables of the class, including
all of its superclasses. The first item in the blob is always the
'isa' instance variable, which points to the object's class. This is
how the runtime knows what kind of object you're dealing with, even
when you pass things around as 'id'. The remaining items are the rest
of the instance variables.
A *pointer* to an object is just an address, like 0x12345678. This is
the address of the first byte in that blob of memory. A pointer
essentially lets you find that object in memory, but it is not the
object. Think of it as the difference between your friend Bob, and
your friend Bob's phone number. You can store Bob's phone number in
every room of your house, but there remains only one Bob. If you then
give Bob a present, the phone number in every room in the house will
reach the Bob who is pleased because you gave him a present.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden