Objective-C’s Roots: Difference Between messaging structure and function calling; and the Runtime

原创 2013年12月01日 23:29:21

Item 1: Objective-C’s Roots

Objective-C is similar to other object-oriented languages, such as C++ and Java, but also differs in many ways. If you have experience in another object-oriented language, you’ll understand many of the paradigms and patterns used. However, the syntax may appear alien because it uses a messaging structure rather than function calling. Objective-C evolved from Smalltalk, the origin of messaging. The difference between messaging and function calling looks like this:

// Messaging (Objective-C)
Object *obj = [Object new];
[obj performWith:parameter1 and:parameter2];

// Function calling (C++)
Object *obj = new Object;
obj->perform(parameter1, parameter2);

The key difference is that in the messaging structure, the runtime decides which code gets executed. With function calling, the compiler decides which code will be executed. When polymorphism is introduced to the function-calling example, a form of runtime lookup is involved through what is known as a virtual table. But with messaging, the lookup is always at runtime. In fact, the compiler doesn’t even care about the type of the object being messaged. That is looked up at runtime as well, through a process known as dynamic binding, covered in more detail in Item 11.

The Objective-C runtime component, rather than the compiler, does most of the heavy lifting. The runtime contains all the data structures and functions that are required for the object-oriented features of Objective-C to work. For example, the runtime includes all the memory-management methods. Essentially, the runtime is the set of code that glues together all your code and comes in the form of a dynamic library to which your code is linked. Thus, whenever the runtime is updated, your application benefits from the performance improvements. A language that does more work at compile time needs to be recompiled to benefit from such performance improvements.

Objective-C is a superset of C, so all the features in the C language are available when writing Objective-C. Therefore, to write effective Objective-C, you need to understand the core concepts of both C and Objective-C. In particular, understanding the memory model of C will help you to understand the memory model of Objective-C and why reference counting works the way it does. This involves understanding that a pointer is used to denote an object in Objective-C. When you declare a variable that is to hold a reference to an object, the syntax looks like this:

NSString *someString = @"The string";

This syntax, mostly lifted straight from C, declares a variable called someStringwhose type is NSString*. This means that it is a pointer to an NSStringAll Objective-C objects must be declared in this way because the memory for objects is always allocated in heap space and never on the stack. It is illegal to declare a stack-allocated Objective-C object:

NSString stackString;
// error: interface type cannot be statically allocated

The someString variable points to some memory, allocated in the heap, containing an NSString object. This means that creating another variable pointing to the same location does not create a copy but rather yields two variables pointing to the same object:

NSString *someString = @"The string";
NSString *anotherString = someString;

There is only one NSString instance here, but two variables are pointing to the same instance. These two variables are of type NSString*, meaning that the current stack frame has allocated 2 bits of memory the size of a pointer (4 bytes for a 32-bit architecture, 8 bytes for a 64-bit architecture). These bits of memory will contain the same value: the memory address of the NSString instance.

Figure 1.1 illustrates this layout. The data stored for the NSString instance includes the bytes needed to represent the actual string.


Figure 1.1 Memory layout showing a heap-allocated NSString instance and two stack-allocated pointers to it

The memory allocated in the heap has to be managed directly, whereas the stack-allocated memory to hold the variables is automatically cleaned up when the stack frame on which they are allocated is popped.

Memory management of the heap memory is abstracted away by Objective-C. You do not need to use malloc and free to allocate and deallocate the memory for objects. The Objective-C runtime abstracts this out of the way through a memory-management architecture known as reference counting (see Item 29).

Sometimes in Objective-C, you will encounter variables that don’t have a * in the definition and might use stack space. These variables are not holding Objective-C objects. An example is CGRect, from the CoreGraphics framework:

CGRect frame;
frame.origin.x = 0.0f;
frame.origin.y = 10.0f;
frame.size.width = 100.0f;
frame.size.height = 150.0f;

CGRect is a C structure, defined like so:

struct CGRect {
  CGPoint origin;
  CGSize size;
typedef struct CGRect CGRect;

These types of structures are used throughout the system frameworks, where the overhead of using Objective-C objects could affect performance. Creating objects incurs overhead that using structures does not, such as allocating and deallocating heap memory. When nonobject types (intfloatdoublechar, etc.) are the only data to be held, a structure, such as CGRect, is usually used.

Before embarking on writing anything in Objective-C, I encourage you to read texts about the C language and become familiar with the syntax. If you dive straight into Objective-C, you may find certain parts of the syntax confusing.

在ICC中编译过程出现:):[warning] calling a function without prototype may cause runtime erro

在ICC中写程序 编译过程中出现如下警告 :[warning] calling a function without prototype may cause runtime errors if the...
  • xiayufeng520
  • xiayufeng520
  • 2012年08月22日 08:39
  • 1733

The difference between the request time and the current time is too large.

报错信息: [Error Code]:RequestTimeTooSkewed, [Message]:The difference between the request time and the ...
  • hraceo
  • hraceo
  • 2017年01月09日 11:07
  • 1160

The difference between Failure and Success

The difference between failure and sucess is the difference between doing something almost right and...
  • bright60
  • bright60
  • 2006年05月18日 19:23
  • 1432

What's the difference between update and upgrade

UPDATE: update is used to download package information from all configured sources. UPGRADE: upgra...
  • toyijiu
  • toyijiu
  • 2017年10月16日 14:53
  • 132

The Differences Between Java, C And C++

if (!window.netshel_ord) { netshel_ord=Math.random()*10000000000000000;}if (!window.netshel_tile) { ...
  • jianxiong8814
  • jianxiong8814
  • 2007年09月25日 12:21
  • 1203


http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=20776139&id=3279579 KVM笔记! kvm原理学习: L...
  • forevertingting
  • forevertingting
  • 2015年08月12日 21:01
  • 491

What's the difference between C++ and Python

In Principle 1. C++ is a "statically typed" language, while Python is a "dynamically typed" languag...
  • bachelorchen
  • bachelorchen
  • 2015年07月20日 11:07
  • 224


Objective-C 方法和选择器有何不同?(Difference between method and selector?) selector是一个方法的名字,method是一个组合体,包...
  • qcx321
  • qcx321
  • 2016年08月06日 09:23
  • 1008

Objective-C Runtime Messaging

  • y550918116j
  • y550918116j
  • 2015年09月23日 15:31
  • 545

What is the difference between L1 and L2 regularization?

今天讨论班一个师姐讲到L1 norm还有L2 norm 的regularization问题,还有晚上和一个同学也讨论到这个问题,具体什么时候用L1,什么时候用L2,论文上写道一般当成分中有几个成分是p...
  • lansatiankongxxc
  • lansatiankongxxc
  • 2013年10月26日 01:17
  • 2586
您举报文章:Objective-C’s Roots: Difference Between messaging structure and function calling; and the Runtime