[to] drill down into the C-language memory area allocation (each segment of the process)

Source: Internet
Author: User

In general, an executable binary (or, more specifically, a process unit under the Linux operating system, called a task in uc/osii) has 3 parts when it is stored (without calling into memory), which is the code snippet (text), the data segment, and the BSS segment, respectively. These 3 parts together make up the executable program's file

C Language Executable code structure

Name Content
Code snippet Executable code, string constants
Data segment Global variable initialized, global static variable initialized, local static variable, constant data
BSS segment Global variable not initialized, global static variable not initialized
Stack Local variables, function parameters
Heap Dynamic memory allocation

(1) Code snippet (text segment): A machine instruction that holds CPU execution. Typically, code snippets are shareable, which makes it possible for programs that require frequent execution to have only one copy in memory. Code snippets are also usually read-only, which prevents other programs from accidentally modifying their instructions. In addition, the code snippet also plans the memory space information requested by the local data.

Code Segment/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 runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as String constants.

(2) data segment: or the global initialization data segment/static data segment (initialized. Segment/data segment). The segment contains global variables that are explicitly initialized in the program, static variables (including global static variables and local static variables), and constant data.

(3) uninitialized data segment: also known as BSS (Block Started by Symbol). The segment is stored in a global uninitialized variable, and a static uninitialized variable.
When a program is loaded into a memory unit, two additional domains are required: The heap domain and the stack domain.

(4) Stack: The value of the parameter that holds the function, the value of the local variable, and the context in which the current task is stored when the task is toggled.

(5) heap segment (heap): used for dynamic memory allocation, which is the memory space that is managed using the Malloc/free series functions.
When an application is loaded into memory space execution, the operating system is responsible for loading code snippets, data segments, and BSS segments, and allocating space in memory for those segments. The stack segment is also assigned and managed by the operating system, without the programmer's display, and the heap segments are managed by the programmer itself, i.e. the application and freeing of space is displayed.

In addition, the executable program has the corresponding program properties at run time. These property pages are managed and maintained by the operating system when there is operating system support.
The following is a sample program code that has been written in the code:

/*code Snippets, data segments, and BSS segments store variable types*/#include<stdio.h>Const intG_a =Ten;//Code SnippetintG_b = -;//Data SegmentStatic intG_c = -;//Data SegmentStatic intG_d;//BSS SegmentintG_e;//BSS SegmentChar*P1;//BSS SegmentvoidMain () {intLocal_a;//Stack    Static intLocal_c =0;//Data Segment    Static intLocal_d;//Data Segment    Char*P3 ="123456";//123456 in the code snippet, p3 on the stackP1 = (Char*) malloc (Ten);//heap, allocated 10 bytes of area in the heap areastrcpy (P1,"123456");//123456{post.content} is placed in a constant area and the compiler may optimize it with the "123456" that P3 points toprintf"\ n"); printf ("code snippet, global initialization variable, read-only const, G_A, addr:0x%08x\n", &g_a); printf ("\ n"); printf ("data segment, global variable, initialize G_b, addr:0x%08x\n", &G_b); printf ("data segment, static global variable, initialization, G_c, addr:0x%08x\n", &G_c); printf ("\ n"); printf ("BSS segment, global variable, uninitialized g_e, addr:0x%08x\n", &g_e, g_e); printf ("BSS segment, static global variable, uninitialized, G_d, addr:0x%08x\n", &g_d); printf ("BSS segment, static local variable, initialization, Local_c, addr:0x%08x\n", &Local_c); printf ("BSS segment, static local variable, uninitialized, local_d, addr:0x%08x\n", &local_d); printf ("\ n"); printf ("stacks, local variables, local_a, addr:0x%08x\n", &local_a); printf ("\ n"); printf ("Heap, malloc allocates memory, p1, addr:0x%08x\n", p1);}

[to] drill down into the C-language memory area allocation (each segment of the process)

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.