Drill down into the C language memory area allocation (segments of the process)

Source: Internet
Author: User
Tags constant printf
In general, an executable binary program (more specifically, in a Linux operating system, a process unit, called a task in uc/osii, has 3 parts when it is stored (not transferred to memory), which is the code snippet (text), the data segment, and the BSS segment. Together, these 3 sections form the document of the executable program

C Language Executable code structure

name content
code snippet   executable code, string constants
data segment   initialized global variables, initialized global static variables, local static variables, constant data
BSS section & nbsp; global variable not initialized, global static variable
Stack   Local variables, function arguments
heap   Dynamic memory allocation

(1) Code snippet (text segment): The machine instruction that holds the CPU execution. Often code snippets are shareable, which makes it necessary for programs that need to be executed frequently to have only one copy in memory. Code snippets are also generally read-only, which prevents other programs from accidentally modifying their directives. In addition, the code snippet also plans the memory space information that the local data requests.

Code Snippets (Segment/text segment) typically refer to a single area of memory used to hold program execution code. The size of this area is determined before the program is run, 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 possible to include some read-only constant variables, such as String constants.

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

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

(4) stack segment (stack): holds the function's parameter value, the local variable's value, as well as when carries on the task to switch, holds the current task the context content.

(5) heap segment (heap): used for dynamic memory allocation, that is, the memory space managed using the Malloc/free series functions.
When an application is loaded into memory space, the operating system is responsible for the loading of code snippets, data segments, and BSS segments, and allocates space for those segments in memory. The stack segment is also allocated and managed by the operating system without the programmer being displayed; The heap segment is managed by the programmer, that is, the application and free space that is displayed.

In addition, the executable program has the appropriate program properties at run time. These property pages are managed and maintained by the operating system when they are supported by the operating system.
Here is the sample program code, which is indicated in the code:

Copy CodeThe code is as follows:




/* Code snippet, data segment and BSS segment storage 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 might optimize it with the "123456" that P3 points to


printf ("n");


printf ("Code snippet, global initialization variable, read only const, G_A, Addr:0x%08xn", &g_a);


printf ("n");


printf ("Data segment, global variable, initialization 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, uninitialized 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, uninitialized, Local_d, Addr:0x%08xn", &local_d);


printf ("n");


printf ("Stacks, local variables, local_a, Addr:0x%08xn", &local_a);


printf ("n");


printf ("Heap, malloc allocating memory, p1, Addr:0x%08xn", p1);


}

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.