Talk C Chestnut Bar (131th: C language Instance--c program memory layout three)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the C program memory layout example, let's go on to this example. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, on the C program memory layout example, we have introduced in the previous two chapters back, this time we will be the previous chapter back in the content of the summary and hints.

Memory Layout Summary

The memory layout of the C program consists of four partitions: the code area, the data area and the BSS, the heap area, and the stack area . You can use Readelf-s filename to view the memory addresses of each partition. These four partitions are extended in memory from a low address space to a high address in turn. Again, we use the diagram in the previous chapter to show you visually and make a comprehensive summary of these partitions.

    • Code Area: The code that hosts the program, in the low address space of memory.
    • Data area: Its address space is located above the code area, mainly the variables in the program, but the function of the local non-static variable is not in the region, but in the stack area. For different types of variables, storage areas are different, more detailed division, please refer to the following data and BSS related information.
    • Data: A global variable and a local static variable that are initialized in the main storage program. Where global variables are not distinguished from static and non-static, as long as the global variables are in the region.
    • BSS: a global variable and a local static variable that are not initialized in the main storage program. Where global variables are not distinguished from static and non-static, as long as the global variables are in the region.
    • Heap Area: located above the data area, the size of the heap is not fixed, it mainly stored in the program dynamically allocated memory. The allocation and recycling of the area is controlled by the programmer itself and therefore prone to problems.
    • Stack Area: located above the heap area, the size of the stack is not fixed, it mainly stores the function of the local non-static variables and function calls related information. The area is managed by the system and cannot be controlled by the programmer.

After summing up the contents of the partition, we combine the examples in the previous chapter to summarize the distribution information of the variables in memory in the example, and we can see that the distribution of the variables in the memory is exactly the same as what we summarized above.

内存分区                分区起始地址       分区中存放的变量和代码栈区:                  0xbfde3000   存放函数和局部变量:la1,la2,i堆区:                  0x0964d000   存放动态分配的内存空间:p所指向的空间.数据区中的bss区:        0x0804a038   存放程序中未初始化的全局变量和局部静态变量:ga1,static_la1数据区中data区:         0x0804a028   存放程序中初始化的全局变量和局部静态变量:ga2,static_la2代码区:                0x080483e0    存放程序的代码
Memory Layout Details

In addition to the summary, we have some small details to share with you. We hope to draw your attention to:

    • 1. There is a green line at the boundary of the heap area and the stack area in the memory layout diagram. It indicates that the size of the heap and the stack is changing, and that they do not have a fixed size like the code area and the data area.
    • 2. The memory space of the heap is extended from a low address to a high address, while the memory space of the stack is extended from a high address to a low address. Although they are all partitions that can vary in size, they are the opposite in the direction of zoning changes.
    • 3. The address space of the code area and data area in the program is fixed and does not change as the program runs. However, the address space of the heap area and the stack area in the program is changed dynamically. Already have the careful crossing found, we in the previous example, ran two times, the variable address in the data area is exactly the same in the two run results, but the variable address in the heap and the stack area is not the same in two run results. This is the best proof.
Outside the memory layout

Crossing, the saying is good, lookers, onlooker sees most. When we look at the various partitions in memory, let's jump out of these partitions and do some explaining from the whole memory point of view, hoping to pull everyone out of the puzzle.

    • 1. The addresses we say in these chapters refer to virtual memory addresses, as described in 129.
    • 2. The layout of the memory in addition to the four partitions we introduced, there are other partitions. Only these four partitions are more closely related to the code.
    • 3. Use the Readelf tool to view the partition information in the executable file, but only the S option of the command is used here, the other options are not introduced, and you can explore it yourself.
    • 4. View memory-related information by/proc the files in the virtual directory, mainly cmdline,maps,status. Readelf is looking at static information that the program has fixed, such as heap, stack These dynamic information, you need to view the files in the proc directory, the files in this directory provides real-time information when the program runs.

Everybody crossing, the example of C program memory layout Let's talk about it here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (131th: C language Instance--c program memory layout three)

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.