C language entry to C ++ master: C language entry function (6)

Chapter 7 Functions

Section 6 Storage and reference of variables, internal functions and external functions

1. Variable storage category

From the perspective of the time (survival) of variables, we can divide the variables into: static storage variables and dynamic storage variables,
which leads to static storage methods and dynamic storage methods

  • Static storage variables: Variables that allocate fixed storage space during program execution.This method of allocating variables is called static storage.

  • Dynamic storage variables: Variables that dynamically allocate storage space during program execution.This method of allocating variables is called dynamic storage

  • Global variables: placed in a static storage area, when the program starts to execute, allocate storage areas to global variables, and release these storage areas after the program execution is completed.
    During the execution of the program, it occupies a fixed storage unit instead of dynamically allocated freed

  • The data stored in dynamic memory
    a) function parameter, function parameter is regarded as a local variable
    b) local variables inside a function defined variable
    c) a data field and the return address of the function call, etc.
    In general, these The data is allocated at the beginning of the function call, and memory space is allocated. After the function is called, these spaces are released. These allocations and releases are dynamic.
    If the same function is called twice, the storage space allocated to the local variables of this function The address is different

2. How local variables are stored

  • Traditional situation:
    local variables of functions: storage space is allocated when the function is called, and the occupied storage space is automatically released after the function execution is completed
#include <iostream>

void functionTest(){
    int c = 4;
    printf("c = %d\n", c);
    c++;
    return;
}

int main() {
    functionTest();
    functionTest();
    functionTest();
    return 0;
}
  • Special case:
    local static variable: Explained with static, it can retain the original value, the occupied storage unit is not released,
    and the value of the variable is the value at the end of the last function call when the function is called next time

  • Static local variable declaration
    a) in the static storage area allocated storage unit does not release during program run
    b) local static variables Initialization at compile time, only given once the initial value, when the program is running it already has the initial value,
    after the initial value when not re-program call, but the call value after the end of the last function
    c) the definition of local static variable, if not given the initial value, the compiler automatically assigned an initial value of 0, and ordinary local variables , If no initial value is assigned,
    the ordinary local variable is an indeterminate value.
    D) Although local static variables still exist after the function call ends, other functions cannot be referenced
    e) Disadvantages: long-term occupation of memory and lower program Readability
    f) Conclusion: Do not use local static variables unless necessary

3. Cross-file reference of global variables

  • extern: At the head of the global variable, make an external variable description, indicating that the variable that appears here is an external variable that has been defined in other files.This
    file does not need to allocate memory for it. Outside of the function

  • Add static before defining the global variable, then the global variable can only be used in this file

4. Cross-file calling of functions

According to the ability to be called by other source files, the functions are divided into internal functions and external functions

#### Internal function:
can only be called by other functions in this file. When defining an internal function, add a static before the function definition, the form is as follows:
static type identifier function name (formal parameter list) {…},
internal Functions are also called "static functions": use internal functions, which can be limited to the file

External function

If a function definition does not use static, it is an external function, you can also add extern, but the function defaults to extern mode.
Extern type identifier function name (formal parameter list) {…}, extern can be omitted

Add a function declaration at the head of the file to use

5. Summary of static keyword usage

  • When static is used when defining a variable inside the function, the variable will be saved in the static storage area and initialized at compile time. If not initialized, the variable will be initialized to 0, and the next time the function is called, the variable The value of the result of the last call

  • Add static before the global variable, so that the variable can only be used in this file, not in other files

  • Before the function definition, add static, so that the function can only be called in this file, not in other files

Published 359 original articles · praised 248 · 660,000 views

Guess you like

Origin blog.csdn.net/Felaim/article/details/105666149