C ++ life cycle and scope

Life Cycle and Scope

  • Variable Category

    • Local variables (declared in a function) and global variables (declared outside all functions)
    • Static variables and non-static variables
  • Use of variables

    • Scope: You can use all the statements of the variable sum
    • Life Cycle: time a variable that can be used
  • Non-static local variable

    • Scope include all statements inside the function
    • Life cycle is a function of time of the first performance
    • Similar villa furniture (a large residential area of ​​the entire program, which a number of files compared several cells, the equivalent function of a cell villa), other people can not use the villa of the furniture, and only this villa well constructed, the furniture was present. Villa demolished, the furniture was gone too.
  • Static local variables

    • Functions included in the scope of all statements
    • Life cycle is the execution time of the entire program (and can be used before the end of function execution)
    • Similar to the land below the villa, the only villa can be used, but the land has always been there may have been used, not only with the villa after villa land after the demolition of the remains, still can be used.
  • Non-static global variables

    • Scope includes all statements in the program
    • Life cycle is the execution time of the entire program
    • Similar to the road outside the cell, the road is public, everyone can use it. In addition, these roads in the absence of these cells also exist, residential demolition, the road still exists, still can be used.
  • Static global variables

    • Scope file contains all statements
    • Life cycle is the execution time of the entire program
    • Similar to the district where the road between cells are mutually closed, another cell can not use this Road area, in addition to the presence of residential roads are also not affected by the villa.
  • Precautions

    • Global variables and static variables are initialized only once (regardless of where they are written in) in the beginning of the program
    • Global and static variables are automatically initialized to 0 (if not explicitly initialized)
    • If there are no non-static local variable initialization, then its content is random
  • The meaning of life cycle in the calculation of Fibonacci numbers Fibonacci sequence reflect on efficiency

//用递归的思想来处理斐波那契数列
#include <stdio.h>
int fei(int sn) {
    if (sn <= 1 ){
        return 1; //分支处理递归函数不可处理的分解后的问题
    }
//分解问题:要想算编号为 sn 的数字,需要算出编号为 sn-1 和 sn-2 的数字,然后相加
    return fei(sn -2) + fei(sn -1); //假设递归函数已经写好,分别用参数sn-2 和 sn-1去调用fei
}

int main() {
    int sn = 0;
    printf("请输入编号: ");
    scanf("%d", &sn);
    printf("结果是%d\n", fei(sn));
    return 0;
}

To calculate the number of the desired number 40 is the number of counted twice numeral 38, the digital calculating number 36 three times, the number 10 may calculate the number thousand times.
Here Insert Picture Description
Below to improve the speed of this process, using an array to hold the figures have calculated that, to ensure that each number is corresponding to number only once forget

#include <stdio.h>
int fei(int sn) {
    int arr[50] = {0};
    if (sn <= 1 ){
        return 1; 
    }
    //先检查数组里有没有放那个编号对应的数字,这个分支去处理数组里还没有放好数字的情况
    if (!arr[sn - 2]) {
        arr[sn - 2] = fei(sn-2);
    }
    if (!arr[sn - 1]) {
        arr[sn - 1] = fei(sn-1);
    }
    return arr[sn -2] + arr[sn -1]; 
}

int main() {
    int sn = 0;
    printf("请输入编号: ");
    scanf("%d", &sn);
    printf("结果是%d\n", fei(sn));
    return 0;
}

And then found longer because arr [50] is a non-static local variable at run time, there will be present with a lot of memory arrays, each fei function can only use its own array, so this function fei that we will never get past the calculated fei other digital functions from this array. The end result is not only used to do things one did not do less, but also took some time to prepare for such an array of storage area.

Method One: static int arr [50]; // array do not need to initialize static variables are automatically initialized to 0, the life cycle of a static local variable is the execution time of the entire program, which means that all functions with all fei It is the same array.

Method Two: int arr [50]; // do a global variable, but also the life cycle of the execution time of the entire program.

Ka.
Published 23 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/guaiderzhu1314/article/details/104103483