Why should the lock be copied and not saved? When do you not need to copy a block?

Why should the lock be copied and not saved? What is the difference between the two under the hood? Under what conditions do you need to copy a block, if any?

+4
source share
2 answers

Usually, when you assign an instance of a class, it goes into a heap and holds until it is freed. But if you declare a block with text, it will go onto the stack. When the stack frame leaves, so does the block instance - unless you copy it, which creates a resident piece. Basically, if you want to continue using the block after the stack frame is inserted, you need to make a copy somewhere.

+6
source

Blocks are created on the stack and destroyed when execution reaches the end of the current area. To avoid this, you must copy them, which will lead to their copying into a heap. Usually you copy the block that you want to call it at some later point, outside the area in which it was created.

0
source

Source: https://habr.com/ru/post/1479955/


All Articles