Difference and connection between bss, data, and rodata, and difference between bssdatarodata

Source: Internet
Author: User

Difference and connection between bss, data, and rodata, and difference between bssdatarodata

Some people may say that the global memory is a global variable. Is it necessary to introduce it in a specific chapter? Can you play with such a simple thing? I have never gotten into it. Do I write programs differently? Although the global memory topic is not easy to play, it is indeed important. Understanding this knowledge is very helpful for optimizing the time and space of the program. After several such experiences, I decided to take a chapter to introduce it.

As we all know, global variables are stored in the global memory, but they are not necessarily true. Local variables modified with static are placed in the global memory, and their scopes are local, but the life cycle is global. In some embedded platforms, the heap is actually a global variable, which occupies a considerable amount of memory and is allocated twice during runtime.
The difference between global variables and global memory is not emphasized here. In this article, the global emphasis is on its life cycle, rather than its scope, so sometimes the concepts of the two may be exchanged.
Generally, the two global variables defined together are adjacent in the memory. This is a simple common sense, but it is sometimes useful. If a global variable is damaged, you can check the access code of the variables before and after it to check whether there is a possibility of cross-border access.
Among the executable files in ELF format, the global memory includes three types: bss, data, and rodata. The formats of other executable files are similar. By understanding the characteristics of these three types of data, we can make full use of their strengths to achieve speed and space optimization.
1. bss
It cannot be remembered whether bss represents Block Storage Start or Block Started by Symbol. People like me who have never used prehistoric computers can't remember such weird names after all. But it doesn't matter. What's important is that we need to know what features bss global variables have and how to use them.
In layman's terms,Bss refers to the global variables that are not initialized and initialized to 0.. What are its characteristics? 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;
}
[Root @ localhost bss] # gcc-g bss. c-o bss.exe
[Root @ localhost bss] # ll
Total 12
-Rw-r -- 1 root 84 Jun 22 14:32 bss. c
-Rwxr-xr-x 1 root 5683 Jun 22 14:32 bss.exe
The size of the variable bss_array is 4 M, and the size of the executable file is only 5 K. It can be seen that global variables of the bss type only occupy the memory space at runtime, rather than the file space.
In addition, in most operating systems, all bss global variables will be cleared when the program is loaded. However, to ensure program portability, it is also a good habit to manually initialize these variables to 0.
2. data
Compared with bss, data is much easier to understand, and its name implies that data is stored in it. Of course,If the data is all zero, the compiler treats it as bss for optimization consideration.. In layman's terms,Data refers to the non-const global variables that have been initialized (non-zero ).. What are its characteristics? 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] # ll
Total 4112
-Rw-r -- 1 root 85 Jun 22 data. c
-Rwxr-xr-x 1 root 4200025 Jun 22 14:35 data.exe
Only by changing the initialization value to a non-zero value, the file will become more than 4 MB. It can be seen that the global variables of the data type occupy the file space and the runtime memory space.
3. rodata
The significance of rodata is also obvious, ro standsRead onlyThat is, read-only data (const ). Pay attention to the following points for data of the rodata type:
LConstants are not necessarily placed inIn rodata, some immediate numbers are directly encoded in the instruction and stored in the code segment (. text.
LString constant, The compiler willRemove duplicate strings automaticallyEnsure that only one copy of a string exists in an executable file (EXE/SO.
L rodata is inMultiple processes are shared., Which can improve the space utilization.
L in some embedded systems, rodata is stored in ROM (such as norflash,Read ROM memory directly at runtimeAnd does not need to be loaded into RAM memory.
L in an embedded linux systemXIP(Local execution) technology can also be directly read without being loaded into the RAM memory.
It can be seen that setting the data that will not be changed during running to the rodata type has many advantages: sharing among multiple processes can greatly improve the space utilization, it does not even occupy RAM space. At the same time, because rodata is protected in the read-only memory page, any attempt to modify it will be promptly discovered, which can help improve the stability of the program.
4. Variables and keywords
Static keywords are used too much to obscure new users. However, there are two roles in summary: Changing the life cycle and limiting the scope. For example:
L inline Function Modification: Restriction Scope
L modify common functions: Restrict Scope
L modify local variables: Change Life Cycle
L modify global variables: Restrict Scope
The const keyword is quite clear. The variable modified with const is placed in rodata. By default, the string is a constant. For const, pay attention to the following points.
L pointer constant: The data to be pointed to is a constant. For exampleConstChar * p = "abc"; p points to a constant, but p itself is not a constant. You can point p to "123 ".
L constant pointer: the pointer itself is a constant. For example, char *ConstP= "Abc"; p itself is a constant. You cannot make p point to "123 ".
L pointer constant + constant pointer: Both pointer and pointer point to data are constants. Const char * const p = "abc"; both are constants and cannot be modified.
The violatile keyword is usually used to modify the global variables and IO memory shared by multiple threads.. Tell the compiler,Do not optimize these variables into registers., Every timeRead from memory honestlyBecause they may change at any time. This keyword may be uncommon, but do not forget it. Otherwise, an error will not give you any clue for debugging for several days.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.