Talk C chestnuts together (131st back: C language instance -- C program memory layout III)

Source: Internet
Author: User

Talk C chestnuts together (131st back: C language instance -- C program memory layout III)

Hello, everyone. The last time we talked about the C program memory layout example, let's continue with this example. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We have introduced the example of C program memory layout in the previous two chapters, which will summarize and prompt the content in the previous chapter.

Memory layout Summary

C programThe memory layout mainly includes four partitions: Code zone, data zone (data and bss), heap zone and stack Zone. You can use readelf-S filename to view the memory address of each partition. These four partitions expand to the high address in order from the low address space in the memory. We will use the diagram in the previous chapter to present it to you and make a comprehensive summary of these partitions.

Code area:It mainly stores the code of the program, which is located in the low address space of the memory. Data zone:Its address space is located in the code area and mainly stores the variables in the program. However, some non-static variables in the function are not in the area, but in the stack area. For more details about the different types of variables and different storage regions, see the following data and bss information. Data: Stores global variables and local static variables initialized in the program. Global variables do not need to be distinguished between static and non-static variables, as long as they are all in this region. Bss:It mainly stores uninitialized global variables and local static variables in the program. Global variables do not need to be distinguished between static and non-static variables, as long as they are all in this region. Heap zone:Located on the top of the Data zone, the size of the heap zone is not fixed. It mainly stores the dynamically allocated memory in the program. Distribution and recovery in this region are controlled by programmers themselves, so problems are also prone. Stack zone:Located in the heap area, the stack area size is not fixed. It stores local non-static variables in functions and information related to function calls. This region is managed by the system and cannot be controlled by programmers.

After summarizing the partition content, let's take a look at the examples in the previous chapter to summarize the distribution of various variables in the memory in the example, in this example, the distribution of each variable in the memory is exactly the same as the above summary.

Memory partition start address partition storage variables and code Stack: 0xbfde3000 storage functions and local variables: la1, la2, I heap: 0x0964d000 storage dynamic allocated memory space: space pointed to by p. in the data zone, bss: 0x0804a038 stores uninitialized global variables and local static variables in the program: ga1, static_la1 data zone: 0x0804a028 stores the global variables initialized in the program and local static variables: ga2, static_la2 code zone: 0x080483e0 stores the program code
Memory layout details

In addition to the summary, we also have some small details to share with you. I hope to attract everyone's attention:

1. In the memory layout diagram, there is a green line between the heap and stack zones. It indicates that the size of the heap and stack areas is changing. They do not have the same size as the code and data areas. 2. the memory space in the heap area is extended from the low address to the high address, while the memory space in the stack area is extended from the high address to the low address. Although they are all partitions with changeable sizes, they are in the opposite direction. 3. The address space of the code and data areas in the program is fixed and will not change as the program runs. However, the address space of the heap and stack areas in the program changes dynamically. We have carefully read the results and found that the variable addresses in the Data zone are exactly the same in the previous example, however, the variable addresses in the heap and stack regions are different in the two running results. This is the best proof. Out of memory Layout

The audience, as the saying goes, should be on the sidelines. When you carefully observe the various partitions in the memory, let's jump out of these partitions and explain from the perspective of the entire memory, hoping to pull everyone out of the box.

1. The addresses we mentioned in these chapters refer to the virtual memory addresses, which were introduced back in one hundred and twenty-nine. 2. In addition to the four partitions described in the memory layout, there are other partitions. These four partitions are more closely related to the code. 3. You can use the readelf tool to view the partition information in the executable file. However, only the S option of this command is used here. Other options are not described. You can find out for yourself. 4. You can view memory-related information through files in the/proc virtual directory, including using line, maps, and status. Readelf displays static information that has been fixed to the program, such as the dynamic information such as heap and stack. You need to view the files in the proc Directory, which provides real-time information during program running.

Let's talk about the example of C program memory layout. I want to know what examples will be provided later, and I will try again.

Related Article

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.