Objective-C uses dynamic binding, but how?
Asked Answered
M

2

4

I know that Objective-C uses dynamic binding for all method calls. How is this implemented? Does objective-c "turn into C code" before compilation and just use (void*) pointers for everything?

Miramirabeau answered 8/11, 2009 at 16:43 Comment(1)
The objective-c runtime is open source, btw. It makes for an interesting read. objc_msgSend() is actually a little bit of assembly that does the dispatch using a tail call optimization to eliminate the need to rewrite the stack frame upon invocation. Quite fast.Armipotent
V
11

Conceptually, what is going on is that there is a dispatcher library (commonly referred to as the Objective C runtime), and the compiler converts something like this:

[myObject myMethodWithArg:a andArg:b ];

into

//Not exactly correct, but close enough for this
objc_msgSend(myObject, "myMethodWithArg:andArg:", a, b);

And then the runtime deals with all the binding and dispatch, finds an appropriate function, and calls it with those args. Simplistically you can think of it sort of like a hash lookup; of course it is much more complicated that then in reality.

There are a lot more issues related to things like method signatures (C does not encode types so the runtime needs to deal with it).

Vintager answered 8/11, 2009 at 17:4 Comment(0)
G
4

Each Objective C method is implemented "under the hood" as (in effect) a C function. The method has a message (text string) associated with it, and the class has a lookup table that matches the message string with the C function. So when you call an Objective C method, what really happens is you send a message string to the object, and the object looks up the associated C function in its class's method lookup table and runs that.

There's more to it with Objective C, like how objects handle messages they don't understand by forwarding them, how they cache message-to-method lookups, and so on, but that's the basics.

C++ is similar, except instead of the class having a message table, it has something else called a "vtable", and you invoke a method not via a text string, but via its offset into the vtable. This is a form of static binding, and speeds up execution somewhat, but is less flexible than dynamic binding.

Gunslinger answered 8/11, 2009 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.