• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
When you send a message to an object, how does it find the corresponding method?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

When you send a message to an object, how does it find the corresponding method?


  • Subject: When you send a message to an object, how does it find the corresponding method?
  • From: Bob Ueland <email@hidden>
  • Date: Sun, 5 Aug 2007 01:35:09 -0700 (PDT)

When you send a message to an object, how does it find the corresponding method? I have a very vague and unclear picture of what happens.

Suppose I define a class called Fraction which is a subclass of the root NSObject. Then in my main program I create an instance, called myFraction using

    Fraction    *myFraction;
    myFraction = [Fraction alloc];
    myFraction = [myFraction init];

I would like to know how myFraction finds the init method located in NSObject?

As I understand when I build and run my program the Xcode:

1. Compiles my source text into object code (machine-executable instructions).
2. Links it to existing and needed libraries of object code.
3. Reserves needed memory ( for global and static variables, heap, stack, code)
4. Loads the linked code in memory.
5. Starts to execute.

When the line
    myFraction = [Fraction alloc];
is executed what happens?

A message is sent to Fraction. But Fraction is not an object, it’s a class? I know you can send messages to objects but here we are sending to a class. Maybe Objective-C treats classes just like objects. In that case I have a class-object on the heap. But who created that object. In my code I never asked for allocation of memory for a class-object, so the compiler must have created it on its own. Maybe as soon as it sees that I have defined a new class it automatically creates one class-object which later lives in the heap.

Ok, so I’m sending the message alloc to a class-object called Fraction. Now alloc is defined in NSObject and not in Fraction. So how do we find the alloc method? The class-object Fraction must have a pointer to its superclass? As I understand every object has an instance variable called isa, which point to a data structure having a lot of useful pointers. In particular there are three pointers called isa , super_class and *methodLists.

typedef struct objc_class {
struct objc_class *isa;
struct objc_class *super_class;
struct objc_method_list **methodLists;
…
} *Class;

(I'm little confused of this new internal isa pointer. It seems that we have an external isa pointer that points to a structure that has an isa pointer inside)

To find alloc the Fraction class-object follows its *methodLists pointer and finds there is no alloc method. Hence it continues the search by following the pointer super_class and finds the NSObject. Following NSObject’s *methodLists pointer we find alloc, which creates the object myFraction.


Let’s now look at the line
    myFraction = [myFraction init];
What happens when we execute it?

First a message called init is sent to it. Since myFraction is a normal object it should not have any methods. Still there is a *methodLists pointer, but I guess it’s of no use. Since myFraction is no class it has no use of the super_class pointer either. So it follows the internal isa pointer and finds the Fraction class-object and from there the process is as before.

Now I’m making a lot of assumptions and I’m expecting that the most of them are dead wrong. I would appreciate if someone could scatter the fog and explain what really happens :).





____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
_______________________________________________

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

  • Follow-Ups:
    • Re: When you send a message to an object, how does it find the corresponding method?
      • From: "John C. Randolph" <email@hidden>
    • Re: When you send a message to an object, how does it find the corresponding method?
      • From: Uli Kusterer <email@hidden>
    • Re: When you send a message to an object, how does it find the corresponding method?
      • From: Charles Steinman <email@hidden>
  • Prev by Date: Resolution independent cursor
  • Next by Date: Re: When you send a message to an object, how does it find the corresponding method?
  • Previous by thread: Resolution independent cursor
  • Next by thread: Re: When you send a message to an object, how does it find the corresponding method?
  • Index(es):
    • Date
    • Thread