One might say that global memory is a global variable, is it necessary to introduce a special chapter? What can you do with such a simple thing? I never delve into it, do I write a program? The topic of global memory is not a good idea, but it's important to know that this knowledge is useful for optimizing the program's time and space. I decided to take a chapter to introduce it, because of the experience several times.
As you know, global variables are placed in global memory, but not necessarily in the reverse. The local variable that is modified with static is placed in the global memory, its scope is local, but the life time is global. In some embedded platforms, the heap is actually a global variable that occupies a fairly large chunk of memory and allocates it two times at runtime.
Here we do not emphasize the difference between global variables and global memory. In this article, the global emphasis is on its lifetime, not its scope, so it is sometimes possible to swap the concepts of the two.
In general, the two global variables defined together are contiguous in the memory location. This is a simple common sense, but sometimes very useful, if a global variable is destroyed, do not check the relevant variables before and after the access code to see if there is a possibility of cross-border access.
In the elf-formatted executable, global memory consists of three types: BSS, data, andRodata。 The other executable file formats are similar. Understanding the characteristics of these three kinds of data, we can give full play to their strengths, to achieve speed and space optimization.
1. BSS
It is not clear whether BSS represents block Storage start or block Started by Symbol. People like this I have not used those prehistoric computer, after all, can not understand such a strange name, also can't remember. It doesn't matter, but it's important to understand what the BSS global variables are and how to use them.
In layman's words,BSS refers to global variables that are not initialized and initialized to 0。 What is the characteristic of it, let's take a look at the performance of a small program.
int bss_array[1024 * 1024] = {0};
int main (int argc, char* argv[])
{
return 0;
}
[Email protected] bss]# gcc-g bss.c-o Bss.exe
[email protected] bss]# LL
Total 12
-rw-r--r--1 root root June 14:32 bss.c
-rwxr-xr-x 1 root root 5683 June 14:32 Bss.exe
The size of the variable bss_array is 4M, and the executable file size is only 5 K. Thus, the global variables of the BSS type only occupy the memory space of the runtime, not the file space.
In addition, most operating systems, when loading the program, will all the BSS global variables are all zeroed, no need you manually to clear zero. But to ensure the portability of the program, it is also a good practice to manually initialize these variables to 0.
2. Data
Data is easier to understand than BSS, and its name implies that it is stored inside. Of courseIf the data is all zero, for optimization reasons, the compiler treats it as a BSS。 In layman's words,data refers to non-const global variables that have been initialized (not 0)。 What is the characteristic of it, we still look at the performance of a small program.
int data_array[1024 * 1024] = {1};
int main (int argc, char* argv[])
{
return 0;
}
[Email protected] data]# gcc-g data.c-o Data.exe
[email protected] data]# LL
Total 4112
-rw-r--r--1 root root June 14:35 data.c
-rwxr-xr-x 1 root root 4200025 June 14:35 Data.exe
Simply by changing the initialized value to nonzero, the file becomes more than 4M.Thus, the global variables of the data type are the file space and the runtime memory space.
3. Rodata
RodataThe meaning is equally obvious, RO representsRead only, which is read-only data (const). AboutRodataType of data, be aware of the following points:
Lconstants are not necessarily placed onRodata, some immediate numbers are encoded directly in the instruction, and are stored in the code snippet (. Text).
L forstring Constants, the compiler willautomatically remove duplicate stringsTo ensure that a string has only one copy in an executable file (EXE/SO).
LRodatais inmultiple processes are shared between, which can improve space utilization.
L In some embedded systems,RodataIn ROM (e.g. Norflash),read ROM memory directly at runtime, no need to load into RAM memory.
L in embedded Linux system, through a kind of calledXIP(In-place execution) can also be read directly without the need to load into RAM memory.
This shows that the data that does not change during the operation is set toRodataTypes, there are many benefits: sharing across multiple processes can greatly improve space utilization and not even occupy Ram space. At the same time becauseRodataIn read-only Memory Pages (page), is protected, any attempt to modify it will be found in time, which can help improve the stability of the program.
4. Variables and keywords
The static keyword is used so much that it makes the novice obscure. However, there are two ways to sum up,Change the life cycleAndLimit Scope。 Such as:
l Modify the inline function: limit scope
L Modify normal function: Limit scope
L Modify Local Variables: Change Life time
L Modify Global variables: limit scope
The const keyword is fairly straightforward, with const-modified variables placed in theRodata, the string default is constant. For const, pay attention to the following points on the line.
L pointer constant: The data pointed to is a constant. Such asConstchar* p = "ABC"; P points to a constant, but p is not a constant, so you can point P to "123" again.
L constant pointer: the pointer itself is a constant. such as: char*ConstP= "ABC"; P itself is a constant, you can't let P point to "123" again.
L pointer constant + constant pointer: the data that the pointer and the pointer point to are constants. Const char* CONST P = "ABC"; Both are constants and can no longer be modified.
The Violatile keyword is typically used to decorate a multi-threaded shared global variable and IO memory。 Tells the compiler thatdo not optimize this type of variable to register, every time you have tohonestly read from memory, because they can change at any time. This keyword may be more uncommon, but do not forget it, or a mistake let you debug for a few days also get a little clue.
BSS, data, and rodata differences and linkages