Storage space layout of C programs under Linux

Source: Internet
Author: User

A program is essentially composed of BSS, data, and text segments three. You can see that an executable program is divided into sections of code, data areas, and uninitialized data areas when it is stored (not being transferred into memory).

      BSS segment ( uninitialized data area ): In a schema with segment memory management, BSS segments (BSS segment) usually refer to a chunk of memory that is used to hold uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.
    • data segment: In a framework that uses segment memory management, the data Segment) usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation.
    • code snippet: in a schema with segment memory management, the code snippet (text Segment) usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program is run, and the memory area is read-only . In a code snippet, it is also possible to include some read-only constant variables, such as String constants.

The target file generated after the program compiles contains at least these three segments, and the approximate structure of the three segments is as follows:

text segment and data segment have been allocated space at compile time , and the BSS segment does not occupy the size of the executable file, it is the linker that gets the memory . The contents of the
    BSS segment (data that is not initialized) are not stored in program files on disk. The reason is that the kernel sets them to 0 before the program starts running. Only body segments and initialization data segments that need to be stored in the program file. The
    data segment (which has already been initialized) allocates space for the data, and the data is saved to the destination file.

contains the entire segment of the data segment and BSS segment, which is often referred to as the data area.

    executables two more areas at run time: the stack area and the heap area.
    (4) Stack: Automatically released by the compiler, storing the function's parameter values, local variables, and so on. Whenever a function is called, the return type of the function and the information of some calls are stored in the stack. The called function then allocates space on the stack for his automatic variables and temporary variables. A new stack is used for each call to a function. The stack area is increased from the high address bit to the low address bit, is a contiguous memory area, the maximum capacity is pre-defined by the system, the application of the stack space beyond this threshold will prompt overflow, the user can get less space from the stack.
    (5) Heap area: Used to dynamically allocate memory in the address area between BSS and the middle of the stack. The programmer applies for allocation and release. The heap is growing from a low address bit to a high address bit, using a chained storage structure. Frequent malloc/free cause a discontinuity in memory space, resulting in fragmentation. When applying for heap space, the library function searches for a sufficient amount of space available in accordance with a certain algorithm. So the heap is much less efficient than the stack.

The source file that will reflect C corresponds to the storage space:

At this point the program has not been put into memory, but in the case of hard disk storage, BSS does not occupy space at this time. BSS gets a memory space when it is linked.

Indicates that the program is running, that is, the storage layout when the program is in memory:

//main.cintA =0;//Global Initialization ZoneChar*P1;//Global Uninitialized ZoneMain () {Static intc =0;//Global (Static) initialization zone    intb//Stack    CharS[] ="ABC";//Stack    Char*P2;//Stack    Char*P3 ="123456";//"123456\0" in the constant area, p3 on the stack. P1 = (Char*)malloc(Ten); P2= (Char*)malloc( -);//areas that are allocated 10 and 20 bytes are in the heap area. }

Storage space layout of C programs under Linux

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.