Objective-c is one of the main languages for iOS development, and object orientation is one of its features. So what exactly is OC object? Are we really familiar with it? I think a lot of people working on iOS don’t necessarily answer that at a deeper level. So next, let us step by step to uncover its mysterious veil!


The content of this article: OC object principle exploration (2) — OC object essence

PS: Recommended articles

Investigation into OC Object Principle (Part 2) — Analysis of ISA

OC Object Principle Exploration (PART 2) — Union (Common Body) and Bit Field


OC object and structure of the relationship?

  • As we all know, objective-C is implemented at the bottom in C/C++, so objects in OC are converted to one of the C/C++ data structures, then compiled into assembly code, and finally converted to machine-readable binary code.

  • Use the following compile command
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp
Copy the code
  • Convert main.m to the main. CPP file

  • Looking at the C++ file, we can see the structure of the class

  • Class is defined as a structure pointer type

  • You can use class_getInstanceOfSize(CLS) and malloc to see the size of the object and how much memory the system has actually allocated to the object.

  • We know from the function that the size of the NSObject object (actually the size of the ISA pointer) is 8 bytes, which actually opens up 16 bytes of memory.

To sum up, the underlying implementation of the NSObject object is actually a structure NSObject_IMP that contains ISA (an objC_class type pointing to a structure pointer).

OC object size member variables?

  • Create a YTPerson class that inherits from NSObject.

  • YTPerson compiled into C++ code is actually the structure YTPerson_IMP.

The current structure YTPerson_IMP size is 8×5=40 bytes according to the structure memory alignment principle (see OC underlying exploration). According to the object memory alignment principle, the calculation memory with 16 bytes aligned is 16×3 = 48 bytes.

  • In main.m, print the size of the object and the size of the object’s memory.

Analysis:

1. The size of the YTPerson object and the YTPerson_IMP structure is the same as the size of the occupied memory space.

2. The size of a member variable in an object determines how much memory the object actually occupies.

Object size and method relationship?

  • Likewise, add instantiation methods and object methods to the YTPerson object.

  • Print the relevant value of YTPerson again:

The number of methods added to the OC has no effect on the size of the YTPerson object.
  • Why doesn’t the addition of methods have an impact on OC objects? Let’s examine the structure YTPerson_IMP after the method is added.

The size of the object will not be affected by the addition of instantiated methods or class methods to the class.

Iv. Summary and overview

  • 1. The underlying implementation of an OC object is a structure.

  • 2. The size of member variables in the OC object affects the size of the object and the memory space occupied by the object.

  • 3. Instantiated methods and class methods in OC objects have no impact on their size and memory footprint.

This is the beginning of the analysis of the nature of OC objects. If you like my article, please follow me. Another important thing will start laterInvestigation into OC Object Principle (Part 2) — Analysis of ISA.