Depth C language memory region allocation (process segments) detailed _c language

Source: Internet
Author: User
Tags constant
C Language Executable code structure

Name Content
Code Snippets 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): 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 Code code as follows:

/* Code snippet, data segment and BSS segment storage variable type * *
#include <stdio.h>
const int G_A = 10; Code Snippets
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 code snippet, p3 on 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%08x\n", &g_a);
printf ("\ n");
printf ("Data segment, global variable, initialization 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);
}

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.