In a C project, should global variables be assigned initial values?

foreword

(1) Brother Ken raised such a question in the communication group before. My thought at the time was that global variables must be assigned initial values. Isn’t this the basic knowledge of C language when I first learned it? Is there anything to offer for discussion?
(2) However, after I finished writing the RAM, the data will be lost if the power is turned off. Why are the initialized global variables stored in RAM? After analyzing the storage of the program in detail , I have another idea.
(3)
<1>hello all It’s time to open the topic every day again, let’s talk about some technical details today: In your C language project, do you assign initial values ​​to global variables casually? Do you know the difference between assigning an initial value and not assigning an initial value? Furthermore, when should an initial value be assigned, and when can it be indifferent whether to assign an initial value?
<2> On this topic, I will not express my opinion for the time being, and I invite interested friends to discuss and discuss.
<3>I will only share a small story that once appeared in my development team: Once when I was doing a code review in the group, an experienced siege lion who mainly developed microcontroller programs argued with us Very powerful, his proposition is that global variables should not be assigned initial values.
<4> Friends, what do you think?
(4) Note: Before reading this article, it is necessary to lose data when the RAM is clearly powered off. Why are initialized global variables stored in RAM? A detailed analysis of the program's stored knowledge has an understanding. Otherwise, I don't understand.

what is actually discussed in this article

(1) First, we need to know where the global variables are stored. We all know that data storage has five parts : code segment , constant area , static data area , stack , and heap .Global variables are stored in the static data area.
(2) Static data is divided into BSS segment and data segment . The BSS segment is responsible for storing uninitialized global variables and static variables. The data segment stores global and static variables that are uninitialized or explicitly initialized to 0.

insert image description here

(3)Therefore, we can know that what this article is going to discuss is actually what is the difference between the BSS segment and the data segment in the static data area

The storage difference between the BSS segment and the data segment

(1) If you search, you will find that some people say: When the program is compiled, no space will be allocated for the data in the .bss section, but only the size of the space required for recording the data. When the program is executed, memory will be allocated for the data in the .bss segment. In this way, part of the memory space can be saved, further reducing the size of the executable program .
(2) Some people may be confused, should the recording space not take up space? We need to know that for a computer system, accessing a space is actually knowing its address, and then accessing a few bytes at this starting address. Then, we only need to know the starting address and the number of bytes accessed in the program. In this way, the data of the BSS segment is successfully stored.
(3) But the data segment is different. You not only need to record its starting address and the number of bytes accessed in the code, but also need to save its initial value. This value is RW-Data.

insert image description here

(4) At the moment when the MCU is powered on, the RW-Data data stored in the ROM (that is, the data segment in the static data area) will be copied to the designated RAM location. But pay attention, here is copying, that is to say, RW-Data data has a copy of data in RAM and ROM!
(5) After power-on, the program runs, and the code knows the starting position and access space of ZI-Data (that is, the BSS segment of the static data area). Therefore, this can be understood as opening up a space for ZI-Data.

insert image description here

Benefits of not initializing global variables

(1) According to the above, we know that the uninitialized global variables are stored in the BSS segment. The BSS segment does not require ROM to allocate additional space to store the initialized value.This can effectively save ROM space.
(2) However, at this time someone may have to ask, the initial value of the uninitialized variable is unknown, which will cause the program not to have unique properties! And it is contrary to what we just learned in C language.
<1> First of all, we need to know one thing, what is the operating environment when we first learn C language. And what is the development environment used by most embedded engineers.
<2> When we first learned the C language, we all ran it directly on the PC, so it is meaningless for a PC with hundreds of GB of memory to save those few B of data.
<3>The development environment used by most embedded engineers may be tens of KB or even a few KB, which can be saved if possible.
<4> At this time, someone is going to say, if it is not initialized, the variable is unknown. So, it depends on the situation.
(3) According to my understanding, if it is some less important variable, it does not have a particularly large impact on the entire system, and the subsequent code will also assign values ​​to it. Can not be initialized. And there are some variables that need to be self-increasing and self-decreasing, variables that have certain decisive factors for the system. It is better to initialize.

Guess you like

Origin blog.csdn.net/qq_63922192/article/details/131505589