1, the opening

This paper attempts to answer the following questions:

1, heap and stack

2. The __weak modifier

  1. __blockThe modifier

features

There are a lot of blogs that talk about this in great detail.

They don’t fit. Read them for the interview

The interviewer asks: Stacks and stacks

Answer: 1, stack, system distribution, heap, artificial application open; 2, stack, small space, large heap space; 3, stack fast, stack slow; Stack, continuous, heap, discontinuous

They don’t inspire each other very well

The interviewer asks: __weak modifier

The weak table is the global weak table. A few words about what the mechanism is. Describe the application scenario in two sentences

Objc_initWeak and objc_storeWeak. Objc_storeWeak. Objc_storeWeak.

As for the other, it’s not contagious, okay

The average tech blog is good to read, not to talk about.

(See, not necessarily understand.

It is not convenient to organize language exposition in time during the interview.

Summary: mostly on the net, not friendly to small white.

With this article, small white friendly. Read the first iOS interview

C… /iOSInterviewQuestions

According to read:

When the interviewer asks you a question, read the following,

With answers ranging from 20 seconds to minutes, the interview time glided by

1, heap and stack

  • Stack, function stack. Function operation, is to stack the function frame by frame, into the calculation. After the calculation is complete, the stack operation is performed.

Stack of memory, system maintenance. To expand the scope of a variable, place it on top of the heap.

  • Heap, object heap. Mainly put objects, the original programmer manually manage the memory of objects on the heap, now with ARC to help manage.
Supplement:

Stacks and heaps live in the computer’s RAM.

A stack that holds functions, temporary variables, and reference objects. When evaluating function frames, we use temporary variables.

Stack and heap belong to the same memory, why is the stack faster than the heap?

A: Memory management, add, delete, change and check.

  • Stack to heap block, mainly reflected in the addition, that is, memory allocation:

Stack, continuous. Create a variable, which is the top of the stack growing. Get the pointer to the stack and move the fixed length. The operation is simple

Heap, memory discontinuous. Query the free List to find the free area each time you add. overhead

The free List, which keeps track of free chunks of memory.

Call malloc, free list to see which memory block is larger than the required memory size, return the address of that memory block, and record that the memory block is occupied

With the subsequent… (dynamic array expansion, etc.), the original memory block allocated is not enough, need to add memory, the operation on the heap is more complex

  • Then look at check:

Stack, get the first address, calculate the offset.

Heap to query a maintained memory map table. overhead

  • Change and delete, similar

Conclusion:

  • Memory allocation and reclamation on the stack is very simple.

Move the pointer to the stack and return the address of the pointer to the stack

Call the function, add the function frame to the stack, move the top pointer forward,

Calculate completion function, stack clear function frame, stack top pointer moved back.

Stack memory management, close to CPU Instruction (this sentence, a little bit of pressure.. , depending on personal preference)

  • Memory management for the heap, a whole set of policies.

Fast, because it’s easy to operate. During the interview, remember how much, say..

Additional:
  • Ds, data structure

The stacks of a computer behave like stacks in data structures.

The heap of a computer has nothing to do with the heap of a data structure

  • {}, scope

{… }, this is a scope,

That’s one frame on the stack of function frames

2,__weakThe modifier

__weak to avoid memory leaks. Strong references between objects cause that objects cannot be released

__weak does not generate a strong reference, the object to which it points is destroyed, and the pointer is automatically set to nil.

  • create

id __weak cat = obj;

This is a text description. It’s all right

The implementation is objc_storeWeak, which takes the address of obj, the address of the assignment object, as the key,

Add the address of the cat variable with the __weak modifier as the value to the weak table

  • The destruction

If obj is nil, empty all variable addresses corresponding to obj in weak table, including cat

Complete destruction in 4 steps:

A, obtain the address of the abandoned object from the weak table (obtain)

B, set all addresses in the record with __weak modifiers to nil (operation object)

C, delete the weak table from the weak table.

D, delete the old object address from the reference count table

  • The principle of

A, Weak_table contains many entries weak_entry_t, and the number of entries num_entries

An object corresponds to an entry. Weak_entry_t, weak_entry_t has weak reference information of the OC object

id __weak cat = obj;

In this case, the address of OBj corresponds to the DisguisedPtr

Referent of weak_entry_T; // The weakly referenced object

Weak_entry_t has a Union structure, including fixed-length array Inline_Referrers [WEAK_INLINE_COUNT] and dynamic array Weak_Referrer_t * Referrers

It’s full of weak reference object pointer addresses

The address of cat is computed and the hash value is calculated (weakly referencing the object pointer address hash array, add)

WEAK_INLINE_COUNT), weak_entry_t uses fixed-length arrays, whereas weak_entry_t uses dynamic arrays

SideTables contains a lot of SideTables. SideTable contains weak_table, reference count table, and spin lock

Let’s talk about the data structure. Okay. Add, delete, change and check their own play

3,__blockThe modifier

Q: Why can’t blocks modify external temporary variables?

Long like this:
typedef void (^Block)(void); int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... int cats = 10; Block block = ^{ // Variable is not assignable (missing __block type specifier) cats = 20; NSLog(@" I have %@ kittens ", @(cats)); }; block(); } return 0; }Copy the code
A: Blocks access external temporary variables and cannot be modified. Because they are on different function frames on the current stack.

To put it simply, block is an anonymous function that accesses external temporary variables. External temporary variables are passing arguments to this function.

Additional:

By default, the function frame calls the stack

__block

The block scope intercepts variables cats, and no __block is simply passed.

Now we’re passing in a structure. This structure contains two member variables, a pointer to __forwarding and the original value cats

In fact, this structure has five member variables, isa Pointers, minimum. It is useless in this article, so try not to mention it

__forwarding pointer to this structure

cats = 20; The actual implementation is (__cself -> cats -> __forwarding -> cats) = 20;

__cself -> cats is a structure that accesses the member variable cats via __forwarding.

With the member variable __forwarding, it is possible to access the __block variable correctly whether it is configured on the stack or heap

To put it simply, block is an anonymous function that accesses external temporary variables. External temporary variable In this case, the function takes a pointer.

In fact, I’m not so… . It.. A book written by a Japanese bigwig. The interview is still… . On the other hand, the person sitting across from you is… . It’s not.. Is Chris Lattner

Supplementary, other methods

External static and global variables, which are passed to the block through Pointers, can be modified

Static means stored in the data area as a static variable.

Additional:

Auto indicates that it is stored in the stack as an automatic variable.

Function inside temporary temporary variable, default has a keyword auto, useless

Feel online.. Unknown sense li, nonsense..

Finally, this paper is not utilitarian

This article is also called “iOS Development, Don’t understand, some impression?”

This article is for reference only

This article is not rigorous enough.

Little white kind, a little useful

Tip:

  • Basic question, missed answer, dude seems to have no potential

So in this series, try writing a few sentences

  • In the interview, you appear timid and gg

Full of trains,..