In-depth explanation of C language memory region allocation (various sections of the process)

Source: Internet
Author: User

Generally, an executable binary Program (more specifically, it is a process unit in the Linux operating system and is called a task in UC/oⅱ) there are three parts in the storage (not transferred to the memory for running), namely the code segment (text), data segment (data), and BSS segment. These three parts together constitute the file of the executable program

Executable code structure in C Language

Name Content
Code segment Executable code and string constants
Data Segment Initialized global variables, initialized Global static variables, local static variables, and constant data
BSS segment The global variable is not initialized, and the Global static variable is not initialized.
Stack Local variables and function parameters
Heap Dynamic Memory Allocation

(1) code segment (text segment ):The machine command that stores CPU execution. Generally, code segments can be shared, so that programs that require frequent execution only need to have a copy in the memory. Code segments are usually read-only, which prevents other programs from accidentally modifying their commands. In addition, the Code segment also plans the memory space information applied for by local data.

Code segment or text segment is usually a memory area used to store code execution. The size of this area is determined before the program runs, and the memory area is usually read-only. Some architectures also allow code segments to be writable, that is, programs can be modified. In the code segment, it may also contain some read-only constant variables, such as string constants.

(2) data segment ):Or initialized data segment or data segment ). This section contains the global variables, static variables (including Global static variables and local static variables) and constant data that are explicitly initialized in the program.

(3) data segment not initialized:Also known as BSS (Block Started by Symbol ). This section stores the global uninitialized variables and static uninitialized variables.
When a program is loaded into a memory unit, two other domains are required: The heap domain and the stack domain.

(4) stack segment ):Stores the function parameter value, local variable value, and the context content of the current task during task switching.

(5) heap ):It is used for dynamic memory allocation, that is, memory space managed using malloc/free functions.
When the application is loaded into the memory space for execution, the operating system is responsible for loading code segments, data segments, and BSS segments, and will allocate space for these segments in the memory. Stack segments are also allocated and managed by the operating system without being displayed by programmers. Stack segments are managed by programmers themselves, that is, applying for and releasing space in a displayed manner.

In addition, the executable program has corresponding program attributes at runtime. When supported by the operating system, these property pages are managed and maintained by the operating system.
The following is the sample code, which has been commented out in the Code:

Copy codeThe Code is as follows:


/* Variable types stored in code segments, data segments, and BSS segments */
# Include <stdio. h>
Const int g_A = 10; // code segment
Int g_ B = 20; // data segment
Static int g_C = 30; // data segment
Static int g_D; // BSS segment
Int g_E; // BSS segment
Char * p1; // BSS segment
Void main ()
{
Int local_A; // Stack
Static int local_C = 0; // data segment
Static int local_D; // data segment

Char * p3 = "123456"; // 123456 in the code segment, p3 in the stack
P1 = (char *) malloc (10); // heap, allocated 10 bytes in the heap Area
Strcpy (p1, "123456"); // 123456 {post. content} is placed in the constant area, and the compiler may optimize it with "123456" pointed to by p3.
Printf ("n ");
Printf ("code segment, global initialization variable, read-only const, g_A, addr: 0x % 08xn", & g_A );
Printf ("n ");
Printf ("Data Segment, global variable, initialize g_ B, addr: 0x % 08xn", & g_ B );
Printf ("Data Segment, static global variable, initialization, g_C, addr: 0x % 08xn", & g_C );
Printf ("n ");
Printf ("BSS segment, global variable, g_E, addr: 0x % 08xn", & g_E, g_E );
Printf ("BSS segment, static global variable, uninitialized, g_D, addr: 0x % 08xn", & g_D );
Printf ("BSS segment, static local variable, initialization, local_C, addr: 0x % 08xn", & local_C );
Printf ("BSS segment, static local variable, not initialized, local_D, addr: 0x % 08xn", & local_D );
Printf ("n ");
Printf ("Stack, local variable, local_A, addr: 0x % 08xn", & local_A );
Printf ("n ");
Printf ("heap, malloc memory allocation, p1, addr: 0x % 08xn", p1 );
}

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.