2

I've read that, the objective-c programs need objective-c runtime to run.

AFAIK, both C/C++ programs don't require any runtime environments to run. as the generated binary code is being executed directly by the underlying OS.

So this means that Objective-c programs require a redundant layer to run, correct? and If so, is this layer seems like Java VM and .net runtime or seems like Qt runtime (in flavor of some additional libraries)?

EDIT: After some read, I found that, the objc compiler generates some more information in the generated compiled code that is responsible of many things such as method passing (objc_sendMsg(), introspection and others)

Thanks.

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219

1 Answers1

5

The compiled code is native but you need an additional library (the runtime) which does all the object and message handling (lookup, invocation etc.). There is no virtual machine involved. So it is more like QT than Java runtime.

[Update]

Since the C++ message binding behaviour is not obvious to programmers of more dynamic OO languages (e.g.: Objective-C or Smalltalk) - like me - I wrote a small C++ test app which demonstrates the effect of the virtual keyword on the choice of the method to call.

#include <iostream>

class Test1 {
public:
    Test1();
    void test1();
    void test2();
};

class Test2 : Test1 {
public:
    Test2();
    void test1();
    void test2();
};

Test1::Test1() {}
void Test1::test1() { std::cout << "T1:t1" << std::endl; }
void Test1::test2() { std::cout << "T1:t2" << std::endl; }

Test2::Test2() {}
void Test2::test1() { std::cout << "T2:t1" << std::endl; }
void Test2::test2() { std::cout << "T2:t2" << std::endl; }

int main(int argc, char **argv)
{
    Test1 *t11 = new Test1();
    Test1 *t12 = (Test1 *)(new Test2());
    Test2 *t2 = new Test2();

    t11->test1();
    t11->test2();

    t12->test1();
    t12->test2();

    t2->test1();
    t2->test2();

    return 0;
}

An Objective-C programmer would expect a output of

T1:t1
T1:t2
T2:t1
T2:t2
T2:t1
T2:t2

since t12 is actually a Test2 which was casted to Test1. The actual output is

T1:t1
T1:t2
T1:t1
T2:t2
T2:t1
T2:t2

because C++ (by default, i.e. without virtual) statically binds the call to test1 based on the type it knows at compile time which is Test1 (due to the cast).

Tilo Prütz
  • 1,766
  • 3
  • 16
  • 27
  • So, linker will link the complied -object- code with additional libraries of Objc to generate native code. the same way exactly its done in C/C++/Qt correct? – Muhammad Hewedy Sep 09 '11 at 14:28
  • 1
    Yes. But as you mentioned C++ I want to clarify: C++ is (as C) a statically typed language. All method calls and data types are resolved at compile time and no extra processing time is need at runtime to deal with method calls beside the normal function call. Objective-C is a dynamically linked language where a runtime library is needed too deal with objects and their ability to respond to messages (perform functions) at runtime. Also the type of an object is not determined at compile time but at runtime. – Tilo Prütz Sep 12 '11 at 09:13
  • But in case of specifying `virtual`, the dynamic-binding turned on the the type of the object is determined at runtime, Correct? http://en.wikipedia.org/wiki/Dynamic_dispatch#C.2B.2B_Implementation – Muhammad Hewedy Sep 12 '11 at 09:59
  • Yes, for methods declared virtual the actual type is used at runtime to determine the called function from the message-to-method-table. Otherwise (when _not_ using the virtual keyword) the called function is determined at compile time using the object type known to the compiler. E.g.: if you have a method with a parameter of type A on which it calls method M it would _not_ call an overridden implementation of M on an actual parameter value of type B:A unless M is declared virtual in A. – Tilo Prütz Sep 14 '11 at 09:47
  • By `message-to-method-table` you mean `virtual-method table` http://en.wikipedia.org/wiki/Virtual_method_table, correct? BTW, I've wrote a small article illustrate dynamic-binding in C++ vs other dynamic language such as Java/Objc and post it at my blog here http://m-hewedy.blogspot.com/2011/09/dynamic-binding-dispatch-by-example.html – Muhammad Hewedy Sep 14 '11 at 11:28
  • Correct, that was what I meant. – Tilo Prütz Sep 16 '11 at 12:05