FROM : Michael Ash
DATE : Fri Aug 01 22:47:11 2008
On Fri, Aug 1, 2008 at 2:43 PM, Wayne Shao <<email_removed>> 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
DATE : Fri Aug 01 22:47:11 2008
On Fri, Aug 1, 2008 at 2:43 PM, Wayne Shao <<email_removed>> 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
| Related mails | Author | Date |
|---|---|---|
| Wayne Shao | Aug 1, 20:43 | |
| Ken Thomases | Aug 1, 20:47 | |
| Clark S. Cox III | Aug 1, 20:54 | |
| Glenn English | Aug 1, 21:10 | |
| Jens Alfke | Aug 1, 21:18 | |
| Clark S. Cox III | Aug 1, 21:24 | |
| Arthur Coleman | Aug 1, 21:38 | |
| Clark Cox | Aug 1, 22:01 | |
| Giulio Cesare Sola… | Aug 1, 22:07 | |
| Clark S. Cox III | Aug 1, 22:15 | |
| Glenn English | Aug 1, 22:20 | |
| Michael Ash | Aug 1, 22:47 | |
| Sherm Pendley | Aug 1, 23:07 | |
| Daniel Staal | Aug 2, 00:27 | |
| Negm-Awad Amin | Aug 2, 09:05 | |
| Negm-Awad Amin | Aug 2, 09:09 | |
| mm w | Aug 2, 18:50 | |
| mm w | Aug 2, 20:19 | |
| Keary Suska | Aug 2, 22:53 | |
| Negm-Awad Amin | Aug 4, 08:31 |






Cocoa mail archive

