Block Storage Area-how to verify whether a block is on the stack or on the stack to verify the block

Source: Internet
Author: User

Block Storage Area-how to verify whether a block is on the stack or on the stack to verify the block
Block Storage area first, three terms need to be introduced: ● _ NSConcretStackBlock ● _ NSConcretGlobalBlock
● _ NSConcretMallocBlock
The three block storage methods are described as follows: Stack, global, and heap. The isa value in the block object is one of the above values. The following describes which block is stored on the stack, stack, and global. Why is a block an object? Click to open the link ------------ [Key Point 1]: global block ------------ ● define the block outside the function as a global type ● define the block inside the function, however, if no automatic variables are captured, they are global. For example, the following code

typedef int (^blk_t)(int);for(...){    blk_t blk = ^(int count) {return count;};}
Although this block is in a loop, the blk address remains unchanged. This block is in the global segment. Note: For blocks without capturing automatic variables, although _ NSConcretStackBlock is still displayed in the Code converted using clang's rewrite-objc, but this is not actually the case (this may be the optimization of the compiler, I guess, without proof)
------------ [Key Point 2]: Stack block ------------ in this case, it cannot be compiled under non-ARC, and can be compiled under ARC
typedef void (^block_t)() ;-(block_t)returnBlock{    __block int add=10;    return ^{printf("add=%d\n",++add);};}
This is because: block captures the add automatic variable on the stack. At this time, add has become a struct, and the block has a pointer to this struct. That is, if block is returned, it is the pointer to the local variable. This point is precisely determined by the compiler. It can be compiled under ARC because ARC uses autorelease. Another scenario:

-(block_t)returnBlock{    __block int add=10;    block_t blk_h =^{printf("add=%d\n",++add);};    return blk_h;}block_t bb = [self returnBlock];bb();

This Code only uses an automatic block variable that can be edited, but causes the program to crash.
If copy is added when block is returned, the correct value is 11.

------------ [Point 3]: block on the stack -------------- sometimes we need to call the block copy function to copy the block to the stack. See the following code:
-(id) getBlockArray{    int val =10;    return [[NSArray alloc]initWithObjects:        ^{NSLog(@"blk0:%d",val);},        ^{NSLog(@"blk1:%d",val);},nil];}id obj = getBlockArray();typedef void (^blk_t)(void);blk_t blk = (blk_t){obj objectAtIndex:0};blk();
This code will be abnormal in the last line of blk (), because the block in the array is on the stack. Because val is on the stack. The solution is to call the copy method. In this scenario, the ARC will not add a copy for you, because the ARC is not certain, taking a conservative measure: do not add a copy. Therefore, ARC exits abnormally.------------------- [Key Point 4] use of copy ------------------------------------- no matter where the block is configured, copying using the copy method will not cause any problems. In the ARC environment, if you are not sure whether to copy the block, just copy it. ARC will clean the battlefield. NOTE: If copy is called on the stack, copy is copied to the stack, and no copy is called on the global block, increase the block reference count on the stack by calling ---------------- [Challenges to Objective-C advanced programming] ------------------------- I found through Xcode 5.1.1 iOS sdk 7.1 Compilation: not as described in Objective-C advanced programming, block has a huge difference between ARC and non-ARC: int val must be on the stack. I saved the val address, check whether the block is changed before and after calling. The output consistency is on the stack, and the inconsistency is on the stack.
Typedef int (^ blkt1) (void);-(void) stackOrHeap {_ block int val = 10; int * valPtr = & val; // use the int pointer, to check whether the block is on the stack or on the stack, blkt1 s =^{ NSLog (@ "val_block = % d", ++ val); return val ;}; s (); NSLog (@ "valPointer = % d", * valPtr );}
Under ARC, >>>>>>>>>>> the block is directly generated to the stack. Check log: val_block = 11 valPointer = 10 under non-ARC >>>>>>>> the block is still on the stack. View log: val_block = 11 valPointer = 11

The result after the copy operation is called:

-(Void) stackOrHeap {_ block int val = 10; int * valPtr = & val; // use the int pointer to check whether the block is on the stack, or heap blkt1 s = ^ {NSLog (@ "val_block = % d", ++ val); return val ;}; blkt1 h = [s copy]; h (); NSLog (@ "valPointer = % d", * valPtr );}

Under ARC, >>>>>>>>>>> no effect. Val_block = 11 valPointer = 10
In non-ARC mode, >>>>>>>>>> it is copied to the stack. Val_block = 11 valPointer = 10

---------------- [Summary] ----------------- Use this table to represent
Under ARC: There seems to be no block on the stack, either global or non-ARC on the stack: There are three forms: Stack, global, and stack.
Some friends will answer this programming question and place it in the storage area where BLOCK is the first address in the data segment.

DATA SEGMENT
Block db 12, 0,-20, 45,-29, 36, 50, 0,100,-100
; 10 signed numbers (-128 ~ + 127)
Average db?
DATA ENDS

CODE SEGMENT
Assume ds: DATA, CS: CODE
START:
Mov ax, DATA
Mov ds, AX
Xor ax, AX
Lea si, BLOCK
Mov cx, 16
_ ADD:
Add al, [SI]
Adc ah, 0
INC SI
LOOP _ ADD

Mov bl, 16
DIV BL

Mov average, AL

Mov ax, 4C00H
INT 21 H
CODE ENDS
END START

In ICEM, how does one change a block into multiple regions? Can the block divided in ICEM be batch set to a region?

The split block can be divided into multiple parts. As for the approval of a region, you should specify the fluid domain and the solid domain. You can set the body.

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.