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 (BSS segment) usually refers to an area 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 segment-based memory management architecture, data segment usually refers to a piece of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation.
    • Code snippets: In a schema with segment memory management, the code snippet (text segment) usually refers to a piece of memory area that is used to hold the program execution code. The size of this part of the area is determined before the program runs 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:

The text and data segments have been allocated space at compile time, and the BSS segment does not occupy the size of the executable file, which is used by the linker to obtain memory .
The contents of a BSS segment (data that is not initialized) are not stored in a program file 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 (the data that has already been initialized) is the space for which it is allocated and the data is saved to the destination file.

The data segment contains the initialized global variables and their values. The size of the BSS section is obtained from the executable file, and then the linker gets a block of memory of that size, immediately following the data segment. When this memory enters the program's address space, it is all zeroed out. the entire segment that contains the data segment and the BSS segment is often referred to as the data area.

The executable program has two more zones at run time: the stack area and the heap area.
(4) Stack area: Automatically released by the compiler, storing the function 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 for dynamic allocation of memory, located in the middle of the BSS and the address area. 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.c  int a = 0;//global Initialization zone  char *p1;//Global Uninitialized Zone    main ()  {      static int c = 0;//global (Static) initialization zone      int b;//Stack      char s[] = "abc";//stack      char *p2;//Stack      char *p3 = "123456";//"123456\0" in the constant area, p3 on the stack.      P1 = (char *) malloc ();      P2 = (char *) malloc (20); Areas that are allocated 10 and 20 bytes are in the heap area.  }

Storage space layout of C programs under Linux

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.