Pointer and memory address of the alloc object

Create a new LXPerson class and run it to see the results of the code below

LXPerson *p1 = [LXPerson alloc];
LXPerson *p2 = [p1 init];
LXPerson *p3 = [p1 init];

NSLog(@"%@-%p-%p", p1, p1, &p1);
NSLog(@"%@-%p-%p", p2, p2, &p2);
NSLog(@"%@-%p-%p", p3, p3, &p3);
Copy the code

The code results are as follows:

<LXPerson: 0x10050f7c0>-0x10050f7c0-0x7ffeefbff3e8
<LXPerson: 0x10050f7c0>-0x10050f7c0-0x7ffeefbff3e0
<LXPerson: 0x10050f7c0>-0x10050f7c0-0x7ffeefbff3d8
Copy the code

It follows from the discovery that objects P1, P2, and P3 have the same memory, except that their pointer memory addresses are different

  1. What actually creates the object isallocMethods;
  2. initDoes nothing to the pointer;
  3. Pointers P1, P2, and P3 are contiguous, pointing to the same memory address.

As shown in the figure below

So how does alloc open up memory, we can not see the specific internal implementation of alloc through code jump, let’s analyze the underlying problems encountered several low-level exploration methods

Two, the bottom three ways to explore

1. The control key +step into

First go to the [LXPerson Alloc] breakpoint and follow the steps below

Eventually jump to the bottom page and you’ll see that the objc_alloc method is called

2. Symbol breakpoint view the call flow

If we continue debugging and find that we can’t go further, we can add the symbol breakpoint of objc_alloc

Moving on, we find ourselves in the _objc_rootAllocWithZone method

We can continue to add a symbolic breakpoint to the _objc_rootAllocWithZone for further exploration

3. Compile and view the call process

After the code runs to the breakpoint, open Xcode Debug->Debug Workflow->Alawys Show Disassembly

Continuing with the Control key +step into calls the objc_alloc method

By adding a symbolic breakpoint, we know which library the objc_alloc method is in

We can also add the alloc symbol breakpoint directly

Now that we know where objc_Alloc’s source code is, we can debug it in the source code and the process becomes clearer

Three, assembly combined with source code debugging analysis

appleObjc4 source code addressAnd we useObjc4-818.2 -Analysis of the source code, we jump according to the source code, found[LXPerson alloc]Run that will call NSObjectallocMethod, and then continue to call_objc_rootAlloc,callAllocmethods

Then add the alloc, _objc_rootAlloc, and callAlloc symbol breakpoints and run to see the results

The objc_msgSend method is directly called after _objc_rootAlloc. This is caused by compiler optimization, so we will not do too much expansion for the moment

Alloc calls the main flow

After alloc, _objc_rootAlloc, and callAlloc above, the code continues to execute, calling _objc_rootAllocWithZone

Next comes the _class_createInstanceFromZone method, which takes three important steps

1.cls->instanceSizeCalculate the amount of memory required

2.callocApplying for Memory Space

3.objWith the currentclsassociated

Detailed flow chart of AlloC

According to the above analysis, we draw a rough alloC implementation flow chart