Ios:block Write recursion

Source: Internet
Author: User

First, one OC recursion:  

-(int) sum: (int) num {    if0) {        return num;    }     return 1 ];}

The write recursive algorithm only needs to remember two points:

1. There is a clear export

2. When you do not meet the conditions of export, you call yourself

Follow the above ideas with block rewrite:

Static int (^sumblock) (int) = ^ (int  num) {    if0) {        return num;    }     return 1

Note that to do this yourself, you need to be able to accurately find the ' block ' function entry in memory, so you need to use the ' static ' modifier symbol, and nothing else.

In addition, if asked in the interview, be sure to say:

1. Each time you call yourself, the system will open a stack frame record temporary variables and parameters

2. Too many recursion times, stack overflow error will occur

3. Recursive algorithm is not recommended in mobile development, and now the main thread stack area is only 512K

The above test code calls NSLog(@ "%d", Sumblock (1024x768 )); There will be a stack overflow error

Ios:block Write recursion

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.