Deep-Dive into C-language memory area allocations (segments of the process)

Source: Internet
Author: User

Original address: http://www.jb51.net/article/39696.htm

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

(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 snippet, data segment, and BSS segment store variable type */
#include <stdio.h>
const int G_A = 10; Code snippet
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 snippet, p3 on the stack
P1 = (char *) malloc (10); Heap, allocated 10 bytes of area in the heap area
strcpy (P1, "123456"); 123456{post.content} is placed in a constant area and the compiler may optimize it with the "123456" that P3 points to
printf ("\ 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 section, 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 ("Stack, local variable, local_a, addr:0x%08x\n", &local_a);
printf ("\ n");
printf ("Heap, malloc allocates memory, p1, addr:0x%08x\n", p1);
}

Deep-Dive into C-language memory area allocation (process segments) detailed (GO)

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.