ARM architecture and C language (Wei Dongshan) study notes (3) - some questions about local variables, global variables, stack and heap


1. When to use global variables and static variables

Global variables are suitable for situations where data needs to be shared among multiple functions or modules . When defining a global variable, factors such as variable naming convention, scope, and storage type need to be considered to ensure the correctness and readability of the variable.

Static variables are suitable for situations where persistence of data between function calls is required . The scope of static variables is the entire file, can be used in any function in the file, and will only be initialized once during the running of the program, so it can be used to store some data that needs to maintain state.

For multi-process occasions, it is not suitable for global variables. Process security means that in a multi-process environment, multiple processes can access the same resource at the same time without problems such as race conditions. In multi-process programming, since each process has an independent address space, global variables are not shared between different processes, that is, each process will have its own copy of the global variable.
This leads to a problem: if multiple processes access the same global variable at the same time, each process will operate its own copy, which may cause data inconsistency, race conditions, etc., and then affect the correctness and stability of the program . Therefore, the use of global variables for inter-process communication and shared data is unreliable. In order to solve this problem, some inter-process communication mechanisms can be used, such as pipelines, message queues, shared memory, etc., to realize inter-process data sharing and communication.

Second, why do local variables sometimes need to be static? After adding static, is the variable no longer in the stack?

int add_val(volatile int v){
    
    
    static volatile int a=321;
    v=v+a;
    return v;
}

(1) This local variable a is modified by static. When we need to retain the value of the local variable between function calls, or need to share some data between different functions , we can use the static keyword to declare the static local variable . A static local variable defined inside a function will not end its life cycle when the function is called, but will always exist during the running of the program. Its scope is still inside the function, but because its life cycle is longer than that of ordinary local variables, it can keep its value unchanged between multiple function calls . In addition, the initialization of static local variables will only be performed when the function is called for the first time, and subsequent calls will not perform initialization operations again, so it can be used to store some data that needs to maintain state.
(2) Static local variables are stored in the static data area, not on the stack . The static data area is a part of the program and is used to store data such as global variables, static variables, and static constants. Its life cycle is the same as the running cycle of the program, so static local variables always exist during the running of the program.

3. The proportion of KEIL compiled code space

insert image description here
This piece of information is the size information of the program, which includes the following four parts, and the unit is byte:

Code: The size of the code segment, which refers to the executable code part of the program, including functions, instructions, etc. In this example, the size of the code segment is 30646.

RO-data: The size of the read-only data segment, which refers to data such as read-only variables and constants in the program. In this example, the size of the read-only data segment is 2806.

RW-data: The size of the read and write data segment, which refers to the readable and writable variables and data in the program. In this example, the read and write data segment size is 2736.

ZI-data: The size of the uninitialized data segment, which refers to uninitialized readable and writable variables and data in the program. In this example, the size of the uninitialized data segment is 2240.

4. Will too deep recursion cause stack overflow?

Recursion too deep may cause stack overflow . A recursive function will store the current function call information in the stack every time it is called. When there are too many recursions, the stack space may be exhausted, resulting in stack overflow. In most programming languages, the size of the stack space is limited, usually between a few MB and tens of MB. If the number of recursive function calls exceeds the size of the stack space, it will cause a stack overflow. A stack overflow can cause program crashes or unpredictable behavior.

5. What are the code segment and data segment in FLASH?

In embedded systems, programs are usually stored in flash memory (Flash). In flash memory, a program is usually divided into a code segment and a data segment.
The code segment is an area where program instructions are stored, and is usually read-only . The instructions stored in the code segment are the program code translated into machine code by the compiler, and they are executed by the processor during the running of the program.
The data segment is the area for storing static data and global variables with initial values ​​in the program . Data segments are usually divided into read-only data segments and readable and writable data segments. The read-only data segment usually includes non-modifiable data such as constants and character strings in the program; the readable and writable data segment usually includes readable and writable data such as global variables and static variables in the program.
During program execution, program code and data segments are loaded into RAM. The program code is loaded into the instruction memory and the data segment is loaded into the data memory. During the execution of the program, the interaction between the instruction memory and the data memory and the reading and writing of data in the memory are all controlled by the processor.

6. Can the code segment and data segment in FLASH be copied to RAM?

Yes, the code segment and data segment in the program can be copied into RAM. This process is often referred to as "code duplication" or "data initialization" .

The purpose of code duplication is usually to improve the execution efficiency of the program. Due to the slow reading speed of the flash memory, after the code segment in the program is copied to the RAM, the instruction can be directly read from the RAM when the program is executed, which reduces the number of times to read instructions from the flash memory, thereby improving the performance of the program. Execution efficiency .
The purpose of data initialization is to assign initial values ​​to static data and global variables when the program starts. Since the data stored in the flash memory is read-only, it cannot be modified while the program is running. In order to be able to modify these data, these data need to be copied to RAM first. In the process of copying data, you can assign initial values ​​to these data to meet the needs of the program.

Seven, while (1) and for (;;)

while(1) and for(;;) are both statements used to represent an infinite loop.
while(1) means that when the condition in the brackets is true, the statement in the loop will be executed all the time, because 1 is a true value, so this loop will continue to execute until the program is forcibly terminated.
for(;;) means that there is no loop condition, and this loop will continue to execute until the program is forcibly terminated. Its loop structure is equivalent to while(1), but the syntax is slightly more concise.

Eight, #define pi 3.14 and static flaot pi=3.14

#define pi 3.14 is a preprocessor directive that replaces all occurrences of pi in the program before compilation, replacing it with 3.14. The pi defined in this way is a constant whose value is determined during compilation, so its value cannot be modified during program execution. The advantage of this definition method is that it is simple and easy to understand, but the disadvantage is that it does not support type checking, and it is prone to erroneous operations between different types of data. For example, if there is a function whose parameter should be a value of type float, but a pi is passed in when the function is called, a type mismatch error will occur.

static float pi=3.14 is the definition of a static variable, which defines a static variable named pi and initializes it to 3.14. The advantage of this definition is that it can support type checking, and the value of this variable can be modified during program execution. The scope of a static variable is limited to the function or file that defines it, so different functions or files can define static variables with the same name without affecting each other. The disadvantage of this definition is that it needs to occupy memory space, because the memory space of static variables is allocated when the program is running and exists until the end of the program.

9. Is each task of RTOS equivalent to a function? Will the stack be freed?

In RTOS, each task can be regarded as an independent function, which has its own entry point and execution context, and can be run and managed independently. Each task has its own stack space for storing information such as local variables and function call stacks .

When a task is created, the RTOS will allocate a stack space for it to store information such as local variables and function call stacks . When the task execution is completed, the stack space will be released for use by other tasks. Therefore, each task has its own independent stack, and there will be no problems such as stack confusion.

When a task is suspended, the RTOS will save the stack space of the task in the memory, so that the stack space can continue to be used when the execution is resumed next time. When a task is deleted, the RTOS releases the task's stack space for use by other tasks.

Ten, to be added...

Guess you like

Origin blog.csdn.net/qq_53092944/article/details/131105248