Objective-C memory Layout

Source: Internet
Author: User

In my understanding, an object is a piece of memory. This article will discuss the layout issue of an objective-C object in the memory, if a class does not need to inherit some of its specific behaviors from nsobject, it does not need to inherit nsobject. Here I will discuss the limitations in the scope of objects that inherit nsobject classes.

First, let's take a look at the definition of nsobject:

 
1 @ InterfaceNsobject <nsobject> {
2Class ISA;
3}

(Since we are talking about the memory layout, we can leave the definition of the method apart)

In objective-C,@ InterfaceThe keyword can be viewed as the alias of the struct keyword in the C language. Of course, it also has some other functions, such as letting the compiler know the name of an objective-C class after @ interface. But in terms of its memory layout, we simply replace it with struct and remove the protocal definition. Therefore, nsobject is defined as follows:

 
1 StructNsobject {
2Class ISA;
3}

What is this class? In objc. H, we found that it is just a typedef definition of a structure (struct) pointer:

 
1Typedefstruct objc_class * class;

Therefore, the nsobject definition is like this:

 
1 StructNsobject {
2Objc_class * ISA
3}

ISA is "is a". For all classes that inherit nsobject, their objects also have an ISA pointer. What this isa Pointer Points to (call it first) is the definition of the class to which this object belongs.

It's us.ProgramMember nature: what is the definition of object_class? Since the Fruit Company has hidden this definition, I still have a way to use xcode to create a project and set a debug breakpoint at a variable definition, then, you can use xcode GUI or GDB's p command to view its structure. Here I use GDB to print a uinavigationcontroller variable. We can see that it is just a pointer:

1(GDB) P dialunc
2
3$1= (Uinavigationcontroller *)0x8e8be80

We found that there are many things in the pointer, which are roughly as follows (because GDB prints too much content, the ellipsis indicates that some content is omitted ):

 1 (GDB) p * dialunc
2 $ 1 = {
3 <Uiviewcontroller >= {
4 <Uiresponder >= {
5 <Nsobject >= {
6 ISA = 0x1bebc1c
7 }, < No data fields > },
8 Members of uiviewcontroller:
9 _ View = 0xd5dab60 ,
10 _ Tabbaritem = 0x0 ,
11 _ Navigationitem = 0x0 ,
12 _ Toolbaritems = 0x0 ,
13 _ Title = 0x0 ,
14 _ Nibname = 0x0 ,
15 ... (Some Members are omitted here)
16 },
17 Members of uinavigationcontroller:
18 _ Containerview =0xd5dab60 ,
19 _ Navigationbar = 0xd5dad40 ,
20 _ Navigationbarclass = 0x1beb4d8 ,
21 _ Toolbar = 0x0 ,
22 _ Navigationtransitionview = 0xd5d2f10 ,
23 _ Currentscrollcontentinsetdelta = {
24 Top = 0 ,
25 Left = 0 ,
26 Bottom = 0 ,
27 Right = 0
28 },
29 _ Previusscrollcontentinsetdelta = {
30 Top =0 ,
31 Left = 0 ,
32 Bottom = 0 ,
33 Right = 0
34 },
35 ... (Some Members are omitted here)
36 }
37 }

Note that the black characters in the gdb printing result show that the uinavigationcontroller memory first stores the instance variables of the parent class and then the instance variables of the subclass. The ISA pointer at the beginning is defined in nsobject. Since objective-C does not have many inheritance, its memory layout is very simple, that is:There is an ISA pointer at the beginning, and the instance variables of the parent class are stored before the member variables of the subclass, so easy!!! But there is another question. We are curious. What is Isa? The content to be referenced and printed is roughly as follows:

 1 (GDB) p * dialunc-> Isa
2 $ 2 = {
3 ISA = 0x1bebc30 ,
4 Super_class = 0x1bebba4 ,
5 Name =0xd5dd8d0 " ? " ,
6 Version = 45024840 ,
7 Info = 223886032 ,
8 Instance_size = 43102048 ,
9 Ivars = 0x1bebb7c ,
10 Methodlists = 0xd5dab10 ,
11 Cache = 0x2af0648 ,
12 Protocols = 0xd584050
13 }

This is what a class or objc_class structure looks like in the memory. In fact, the definition of this structure was exposed to the user before Objective-C2.0, but in the Objective-C2.0, the fruit company hidden it. After searching on the Internet, it is found that before Objective-C2.0 its definition is roughly as follows:

 1   Struct Objc_class {
2 Class ISA;
3
4 Class super_class;
5
6 Const Char * Name;
7
8 Long Version;
9 Long Info;
10
11 Long Instance_size;
12 Struct Objc_ivar_list * ivars;
13 Struct Objc_method_list ** methodlists;
14
15 Struct Objc_cache * cache;
16 Struct Objc_protocol_list * protocols;
17 }

So simply put, an objc_class object includes a class: parent class definition (super_class), variable list, method list, And protocal.

"Wait." Someone is shouting. "We just said there is an ISA pointer in an object. This pointer is defined as objc_class, is there an ISA in objc-class? "

It is necessary to explain a large text section here: any class definition in objective-C is an object.That is, when the program starts, any class definition corresponds to a piece of memory.During compilation, the compiler generates one and only one "Class Object describing its definition" for each class, that is, the class object mentioned by the Fruit Company ), he is a Singleton, and the so-called object in C ++ is called an instance object ). It is not hard to understand the instance object, but what does the Class Object do? We know that objective-C is a dynamic language, so all instance objects (instace objec) in the program are generated by the objective-C Runtime Library at runtime, this class object is the basis for the Runtime Library to create an instance object.

Let's take a look. So far, we know:Any class that directly or indirectly inherits nsobject has an ISA pointer in its instance object (instacne objec), pointing to its class object ). This class object stores everything about the definition of the class to which this instance object belongs, including variables, methods, and protocols that follow..

Back to the previous question, is there an ISA in the Class Object pointed to by the ISA pointer of this instance object?

The ISA of this Class Object (class objec) still points to an objc-class, which is a "metaclass object" and a class object) the relationship is as follows: the class object contains the instance variables of the class, the definition of the instance method, and the metaclass object) including the class method (static method in C ++) Definition. Fruit companies in class objects and meta objects also include some other things and may add other content in the future. But for us to understand its memory layout, we only need to remember: class objects store information about instance objects (variables, instance methods, etc.), while metaclass objects store information about classes (class versions, name, class method, etc ). It should be noted that the definition of Class Object and metaclass object are both objc_class structures. The difference is only in usage, for example, the method list stores the instance method in the instance object, while the metaclass object) class method is saved ). Official meta object fruit documentation"The objective--C Programming Language"The Top Of The p29 page is described as follows:

Note: The Compiler also builds a metaclass object for each class. it describes the class object just as the class Object describes instances of the class. but while you can send messages to instances and to the class object, the metaclass object is used only internally by the runtime system.

This paragraph seems to be a bit difficult. Let's look at an example. The following uses the memory layout of an instance variable of a class with a 4-layer inheritance relationship as an example.The inheritance relationship is as follows:

Print an instance variable of the D3 class and record the addresses of those ISA and super_class classes. The relationships obtained are as follows:

 

Here I will explain: a rectangle represents an object (object), that is, a piece of memory; an arrow represents a pointer; ISA represents an ISA pointer; Super represents a super_class pointer, these pointers are member variables of the object at the end of the arrow, except for the "D3 Instance Object" (leftmost object ), other objects are created in the memory at the startup of the program and are singleton. class objects and metaclass objects are used differently, it is defined as the objc_class structure.

The memory layout of D3 objects is: instance variables of ISA, D1, instance variables of D2, and instance variables of D3.. The ISA Pointer Points to the "D3 Class Object" in ". If any class C directly or indirectly inherits nsobject or is nsobjectConclusion::

1. The super_class of class C object points to the Class Object of class C parent class, And the super_class of the nsobject class object points to 0x0.

2. The ISA pointer of Class Object of class C points to its metaclass object)

3. the super_class pointer of metaclass object directs to the metaclass object of the parent class. Exception: metaclass object) super_class to nsobject class object ).

4. The ISA pointer of metaclass object refers to the metaclass object of nsobject)

Nsobject Instance Object (although it does not have instance variables and instance methods, but this object still exists) Its super_class points to address 0x0, because nsobject does not have a parent class, which satisfies the above conclusion 1.

The ISA of the Instance Object of nsobject points to the metaclass object of nsobject, which satisfies conclusion 2 above.

Nsobject's metaclass object points to itself, which satisfies conclusion 4 above.

However, the super_class of the nsobject metaclass object points to the nsobject class object. I didn't see any rules or why Apple did this, I can only say "apple just do this, I don't know why" (if anyone knows, please let me know, Thank you ). I think the fruit engineers simply point it to the nsobject class object ), in fact, I don't think that the super_class pointer assigned 0 x can be used (this satisfies the conclusion 3 above, because nsobject does not have a parent class, so its metaclass object super_class points to 0x0, I think this is more unified. Of course, this is just my YY ).

This is the end. You are welcome to make a picture.

References: 1. Apple official documentation"The objective--C Programming Language"

2. http://algorithm.com.au/downloads/talks/objective-c-internals/objective-c-internals.pdf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.