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

Source: Internet
Author: User

Hello, crossing. The last time we were talking about the C program Memory Layout sample, we continue to say this example. Gossip Hugh, words return to the positive. Let's talk about C chestnuts together.

Crossing, on the C program memory layout of the sample, 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. Heap area and stack area . Ability to use Readelf-s filename to view the memory addresses of each partition. These four partitions are extended in memory from the low address space to the high address in turn. Again, we use the diagram in the previous chapter to visually show you, and make a comprehensive summary of these partitions.

    • Code Area: The code that primarily stores the program. is in the low address space of memory.
    • Data area: Its address space is located above the code area. The main store variables in the program, only the local non-static variables in the function is not in the region, but in the stack area. There are different types of variables. The storage areas are also different, more specific 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.

      The global variables do not have to distinguish between static and non-static. Only if 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.

      The global variables do not have to distinguish between static and non-static, only if the global variables are in the region.

    • Heap Area: located above the data area. The size of the heap is not fixed, it mainly stores the dynamically allocated memory in the program. The allocation and recycling of the area is controlled by the program ape itself. So also easy problem.
    • Stack Area: located above the heap area, the size of the stack is not fixed. It mainly stores information about local non-static variables and function calls in the function. The area is managed by the system and the program ape cannot control it.

After you have finished summarizing 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 sample, and we can see that the distribution of the variables in memory in the sample 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 summing up, we need to share some of the other small details to everyone.

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. But in the direction of the change in the division is exactly the opposite.
    • 3. The address space of the code area and data area in the program is fixed. does not change as program execution occurs.

      However, the address space of the heap and stack areas in the program is dynamically changing. There have been careful crossing found, we in the previous example, executed two times. The address of the variable in the data area is exactly the same in the two execution results, but the address of the variable in the heap and the stack is not the same in the two execution results.

      This is the best proof.

Outside the memory layout

Crossing, the saying is good, lookers. Onlooker sees most When you look at each partition in memory in detail. Let's jump out of these partitions and do some explaining from the whole memory perspective, 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. The only four partitions are more closely related to the code.
    • 3. Use the Readelf tool to view the partition information in the executable file, just use the s option of the command only, the other options are not introduced, and you can explore it yourself.
    • 4. View memory-related information by/proc the files in the virtual folder. There are mainly cmdline,maps,status.

      Readelf View is a program has fixed static information, such as heap, stack These dynamic information, you need to view the files in the proc folder, the file in this folder provides real-time information when the program executes.

You crossing. 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)

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.