learn_C_deep_2 (scope and lifetime, local and global variables, most lenient keyword - auto, fastest keyword - register)

Table of contents

Scope and Lifecycle

Scope: - Scope issues - The effective area of ​​the variable

Life cycle: - There is a problem of length of time - The concept of time, when it is opened, when it is released

The difference between scope and life cycle:

local variables and global variables

code block:

Global variables:

local variable:

The difference between global variables and local variables

Most lenient keyword - auto

Fastest keyword - register 

storage pyramid

Knowledge of registers

The essence of register existence

register modifier variable

Register can modify variables


Scope and Lifecycle

Scope: - Scope issues - The effective area of ​​the variable

        Refers to the range in which a variable or function can be accessed in a program . The scope of a variable or function can be global or local. Variables or functions in the global scope are visible to the entire program, while variables or functions in the local scope are only visible to the code block or function that encloses them.

Life cycle: - There is a problem of length of time - The concept of time, when it is opened, when it is released

        Refers to the process of an object or variable from creation to destruction . The lifetime of an object or variable can be static or dynamic. The static life cycle means that the object or variable always exists during the running of the program, while the dynamic life cycle means that the object or variable is created or destroyed at a specific moment.

The difference between scope and life cycle:

1. Different definitions and meanings: Scope refers to the range in which a variable, function or object is visible in the program. For different types of variables or objects, the scope may be different. The life cycle refers to the time when a variable or object exists, from creation to destruction.

2. Different management methods: the scope is determined at compile time, that is, the scope of a variable or object can be determined when the program is compiled. The life cycle is determined at runtime, and the life cycle of variables or objects needs to be managed during program execution.

3. The impact is different: the scope mainly affects the visibility and access rights of variables or objects, that is, it determines where variables or objects can be accessed. The life cycle mainly affects the memory space occupied by variables or objects, as well as the allocation and release of resources.

local variables and global variables

        In a program, the scope of variables can be divided into two types: global variables and local variables.

code block:

        A code block (also called a statement block) is a piece of code surrounded by a pair of braces `{}`, which can contain multiple statements and is considered as a whole. In C language, code blocks are usually used in the following scenarios:

1. As a function body : the code block of the function contains all the statements of the function, and is used to define the execution flow of the function.

2. As the execution body of the control statement : such as if statement, for statement, while statement, switch statement, etc., their execution body is a code block.

3. Scope as a variable : Variables defined in a code block are only valid within the code block, and cannot be accessed beyond the scope of the code block.

For example, here is a simple code block example:

#include <stdio.h>

int main()

{

        int a = 10;

        {

                int b = 20;

                printf("a + b = %d\n", a + b);

        }

//printf("%d\n", b); // error: variable b is out of scope

return 0;

}

In this example, the code block contains two variables a and b, and the variable b is only valid within the code block, and cannot be accessed beyond the scope of the code block. Since the code block has the characteristics of scope, it can be used to control the access rights of variables, thereby improving the security and maintainability of the program.

Global variables:

        Global variables refer to variables that can be accessed anywhere in the program. The global variables defined in the program have a global scope, that is, the variables can be used anywhere in the program, and they exist until the end of the program. Global variables are usually defined at the beginning of the program, for example:

#include <stdio.h>

int global_variable = 10; // global variable

int main()

{

        printf("global_variable = %d\n", global_variable);

        return 0;

}

local variable:

        A local variable refers to a variable defined within a function, statement block, or other scope. It can only be used within the function, statement block, or scope. It cannot be accessed beyond this scope, that is, it has a local scope. For example:

#include <stdio.h>

int main()

{

        for (int i = 0; i < 5; i++)

        {

                printf("%d\n", i);

        }

        return 0;

}

In this example, i is a local variable defined in the main function, which can only be used in the for loop, and cannot be used outside the scope of the for loop.

The difference between global variables and local variables

1. The scope is different: the scope of a global variable is the entire program, which can be accessed and used by all functions in the program; the scope of a local variable is limited to the function, statement block or other limited scope in which it is defined.

2. The life cycle is different: global variables are created when the program starts, and are destroyed when the program ends, so their life cycle is the same as the running cycle of the entire program; while local variables follow functions, statement blocks or other limited domains. They are automatically destroyed when they are terminated, so they have a shorter lifetime than global variables.

3. Different access rights: Global variables can be accessed and used by all functions in the program, so they are vulnerable to misoperation or illegal access; while local variables can only be accessed within their defined functions, statement blocks or other limited areas, so safer.

4. The storage locations are different: global variables allocate memory in the static data area of ​​the program, while local variables allocate memory in the function stack.

Most lenient keyword - auto

        Generally, the variables defined in the code block, that is, local variables, are modified by auto by default, but we generally omit them. Are all variables auto by default? No, it is generally used to modify local variables. Local variables (local variables, automatic variables, temporary variables are all the same thing)

        In C language, the auto keyword is used to explicitly declare variables with automatic storage type, that is to say, the variables declared by using the auto keyword have automatic storage type inside the function, and their life cycle is the same as the function call The cycle is the same. When the program exits from the function, the variable of the automatic storage type is also "destroyed" and its space is released.

         In C language, variables have "automatic storage type" by default. Using the auto keyword to explicitly declare the storage type of the variable as automatic is redundant, so using the auto keyword in C is unnecessary. But it is used like this:

auto int g_val = 0;//error: the declaration of the global scope cannot contain this storage class

#include<stdio.o>

int main()

{

        auto int a = 5;

        //Equivalent to int a = 5;

        return 0;

In the above code, the auto keyword is used to explicitly declare the variable a as an automatic storage type.

Fastest keyword - register 

storage pyramid

        In fact, the CPU is mainly a hardware unit responsible for calculations, but in order to facilitate calculations, the first step generally needs to read data from the memory into the CPU, so the CPU needs to have a certain ability to temporarily store data. Note: The CPU does not read specific data into the CPU until it is currently calculating, which is too slow. Therefore, a set of hardware called registers is integrated in modern CPUs to store temporary data.

Knowledge of registers

        In a computer, a register is a high-speed data storage device, usually a set of hardware circuits inside the CPU, which can quickly access data and execute instructions inside the CPU. Compared to memory, registers are faster because they are usually embedded directly into the CPU chip and used only inside the CPU. The main functions of the registers include:

1. Store the data and address information that need to be accessed when the CPU instruction is executed;

2. Store the return address, function parameters and other information that need to be saved when the function is called;

3. Store the control information inside the CPU such as the program counter (PC);

4. Store other data that needs to be accessed frequently, such as counters, flags, etc.

        The number and function of registers depends on the CPU architecture and model. Some CPUs have only a few registers inside, while others have a large number of registers, which can improve the execution efficiency of programs. In programming, programmers can use assembly language or special compilation instructions to directly access registers and store data into registers. Due to the high speed of registers, when programming, programmers usually try to use registers to store frequently used data to improve the execution speed of the program.

The essence of register existence

        The essence of the existence of registers is to improve the execution efficiency of computer programs . Computer programs need to frequently perform operations such as data reading, calculation, and storage during execution. These operations need to be completed through storage devices such as memory and cache, and the access speed of these devices is relatively slow, which will cause bottlenecks in program execution.

        In order to improve the execution speed of the program, computer designers have developed registers, a kind of cache, to store data and address information that need to be accessed frequently during the calculation process, so as to reduce the number of accesses to storage devices such as memory , thereby improving the execution efficiency of the program. Using registers reduces data transfer and conversion times between different storage devices, allowing programs to execute faster.

register modifier variable

        In programming languages, register is a keyword that instructs the compiler to store a variable into a register. Because the access speed of registers is very fast, storing variables in registers can improve the execution efficiency of programs, especially for variables that need to be accessed frequently.

        Although using the register keyword can tell the compiler to store variables in registers, whether it can actually store variables in registers depends on the specific implementation of the compiler and CPU. In modern computer systems, due to the limited number of registers and the high probability of being stored in registers by other data, the compiler usually chooses dynamically which variables should be stored in registers as needed. It should be noted that since the register is the internal cache of the CPU, its capacity is very limited, so when using the register keyword, you should weigh the difference between using registers and using memory, and make an appropriate choice according to the actual situation to achieve Optimum execution efficiency. In addition, the register keyword is only a hint to the compiler, and the actual storage location depends on the implementation of the compiler and CPU, so it cannot be guaranteed that all variables can be stored in registers.

To summarize the above: the variable modified by register is to put the modified variable into the CPU register as much as possible, so as to achieve the purpose of improving efficiency.

Register can modify variables

        In C language, only the variables of the automatic storage class (that is, local variables) can be modified with the register keyword, because the variables of the static storage class and global variables have been automatically optimized by the compiler, and the number of registers is sufficient to meet the requirements of the program. need.

Notice:

1. Local variables (global variables and static variables will cause CPU registers to be occupied for a long time)

2. It will not be written into the memory (it needs to be written back to the memory for writing, and if it needs to be read and tested later, the register is not necessary to use)

3. High frequency is read (to improve efficiency)

4. If you want to use, please do not use a lot, because the number of registers is limited

5. Since the variable modified by register is stored in the register, the & operation cannot be performed, because the address is a concept related to memory

#include<stdio.h>     int
main()
{     register int pass = 0;     printf("%d", pass);     printf("%p", &pass);//error: "&" on register variable return 0; }




It's over.

Guess you like

Origin blog.csdn.net/qq_64446981/article/details/130187395