Master - Global Memory

xiaoxiao2021-04-10  391

Master - Global Memory

Please indicate the source and the author contact: http://blog.9cbs.net/absurd

Author: Lixian Jing Update Time: 2007-7-9 Some people might say that global memory is a global variable Well, it is necessary to introduce a special chapter to it? So simple, can you play flowers? I have never been refressed, do you not write procedures? Regarding the topic of global memory, although there is no flowering, it is indeed important, understanding these knowledge, is very helpful for the time and space of the optimization program. Because I have this experience several times, I decided to introduce it to a chapter.

As everyone knows, global variables are placed in global memory, but it is not necessarily established. The local variables modified with Static are placed in the overall memory, and its role domain is part, but the life period is global. In some embedded platforms, the stack is actually a global variable, which takes up a considerable amount of memory, and the memory is used twice when running.

Here we do not emphasize the difference in global variables and global memory. In this article, the global emphasizes its lifetime, not its scope, so sometimes the concepts can be interchanged.

In general, two global variables defined together are adjacent in memory in memory. This is a simple common sense, but sometimes useful, if a global variable is destroyed, do not check the access code of the relevant variables before and after, see if there is a possibility of offshore access.

In executable files in ELF format, global memory includes three: BSS, DATA, and RODATA. Other executable file formats are similar. Understand the characteristics of these three data, we can give full play to their strengths and achieve optimization of speed and space.

BSS

It has been remembered whether BSS represents the Block Startment Start or Block Started by Symbol. Like this person who has not used these prehistoric computers, they can't understand the weird name, and they can't remember. However, there is no relationship. It is important that we have to clear what features of BSS global variables and how to use it.

It is popular that BSS refers to global variables that are not initialized and initialized to 0. What is the characteristics of it, let's take a look at a small program.

INT BSS_Array [1024 * 1024] = {0};

Int main (int Argc, char * argv [])

{

Return 0;

} [root @ localhost bss] # gcc -g bss.c -o bss.exe

[root @ localhost bss] # l l

Total 12

-rw-r - r - 1 root root 84 jun 22 14:32 bss.c

-rwxr-xr-x 1 root root 5683 jun 22 14:32 bss.exe

The variable BSS_Array size is 4m, while the executable file is only 5K. It can be seen that the BSS type global variable only accounts for the memory space, not the file space.

In addition, most operating systems, when loading programs, all BSS global variables are cleared, no need to handle zero. But in order to ensure the portability of the program, it is a good habit to initialize these variables to 0.

2. Data

Compared with BSS, DATA is easy to understand, and its name suggests that data is stored inside. Of course, if the data is all zero, in order to optimize, the compiler is treated as BSS processing. It is popular that DATA refers to the global variables that initialize (non-zero) non-Const. What is it characteristic, let's take a look at the performance of a small program. INT DATA_ARRAY [1024 * 1024] = {1};

Int main (int Argc, char * argv [])

{

Return 0;

}

[root @ localhost data] # gcc -g data.c -o data.exe

[root @ localhost data] # l l

Total 4112

-rw-r - r - 1 root root 85 jun 22 14:35 data.c

-RWXR-XR-x 1 root root 4200025 Jun 22 14:35 Data.exe

It is only to change the value of the initialization to be non-zero, and the file becomes more than 4m. It can be seen that the global variable of the DATA type is a file space, and it is occupied by the runtime memory space.

3. Rodata

The significance of Rodata is also obvious, and RO represents read only, ie read-only data (const). About RODATA type data, pay attention to the following points: L constants are not necessarily placed in rodata, and some immediately encodes directly in the instruction, stored in the code segment (.text). l For string, the compiler will automatically remove the repeated string to ensure that a string only has a copy in an executable file (exe / so). l Rodata is shared between multiple processes, which can improve spatial utilization. l In some embedded systems, Rodata is placed in the ROM (such as Norflash), and the ROM memory is read directly when running, and there is no need to load into the RAM memory. l In an embedded Linux system, through a technique called XIP (in place), it is also possible to read directly without the need to load into the RAM memory.

It can be seen that the data that does not change during operation will be set to the RODATA type, there are many benefits: sharing between multiple processes, can greatly increase the space utilization, and do not even occupy the RAM space. At the same time, since Rodata is protected in a read-only memory page (page), any modification attempt to it will be found in time, which can help improve the stability of the program.

4. Variables and keywords

The Static keyword is used too much to make novice blur. However, there are two roles, changing the life and restrictions. Such as: l Modified inline function: Limit action domain L modified normal function: Limit action domain L modified local variable: change the life period L modified global variable: limit field

The Const key is more clear, and the variable modified with const is placed in rodata, the string is constant by default. For Const, pay attention to the following points. l Pointer Constant: The point to which is constant. Such as const char * p = "abc"; P pointing content is constant, but P itself is not constant, you can let P point to "123". l Constant pointer: The pointer itself is constant. Such as: char * const p = "abc"; P itself is constant, you can't let P point to "123". l Pointer constant constant pointer: The data indicated by the pointer and pointer is constant. Const char * const p = "abc"; both are constants, can no longer be modified. Violatile keywords are often used to modify global variables and IO memory for multi-thread sharing. Tell the compiler, do not optimize such variables into the registers, each time you have to read from memory every time because they may change at any time. This keyword may be more uncomfortable, but don't forget it, otherwise a mistake allows you to debug a few days a few days.

转载请注明原文地址:https://www.9cbs.com/read-133237.html

New Post(0)