Linux Process Memory space composition/process memory image __linux

Source: Internet
Author: User

http://blog.csdn.net/yusiguyuan/article/details/45155035

http://javathinker.iteye.com/blog/1733058 Preface

In the development of the lower level, you need to understand how the program is stored.

"Memory is always occupied by the process", which can be understood in this way: The process always requires memory. When fork () or exec () is a process, system kernel will allocate a certain amount of VM to the process, as the memory space of the process, the size of the BSS segment, the data section of the defined global variables, static variables, text section of the character directly, the program itself memory image, etc. There is also the local variable decision for the stack segment. Of course, you can also dynamically allocate memory through functions such as malloc (), and extend heap up. composition

A typical Linux C program memory space consists of the following parts:

Code snippet (. Text): This is where the CPU is going to execute the instructions. Code snippets are shareable, the same code has only one copy in memory, and the segment is read-only to prevent the program from modifying its own instructions because of errors.

Initialize data segment (. Data): Here is a variable in the program that needs to be explicitly assigned an initial value, such as a global variable that is located outside all functions: int val=100 (the global variable initialized to 0 is placed in the. BSS section). It should be emphasized that both of these paragraphs are located in the executable file of the program, and the kernel reads it from the source program file when the EXEC function is invoked to start the program.

Uninitialized data segment (. BSS): Data in this section that the kernel initializes to 0 or null before executing the program. For example, a global variable that appears outside of any function: int sum;

Heap (HEAP): This segment is used for dynamic memory requests in a program, such as the Malloc,new series function that is frequently used to request memory from this segment.

Stack: Local variables in functions and temporary variables that are generated during function calls are saved in this paragraph. Diagram

Example

int a=1; A in the global initialized data area 

char *p1;//p1 in the BSS area (uninitialized global variable) 

main ()

{

    int b;//b is a local variable, in the Stack area 

    char s[]= "ABC"; S is a local array variable, in the Stack area 

                    //"ABC" is a string constant, stored in the initialized data area 

    char *p1,*p2;//P1,P2 is a local variable, in the Stack area 

    char *p3= "123456";//p3 in the Stack area
                       // "123456" is a string constant that is stored in the initialized data area

    static int c=0;//c as local (static) data, the initialized data area 
                    //static local variable is automatically initialized (because the BSS is automatically initialized with 0 or null)

    static int D;   Uninitialized static local variable   

    p1= (char*) malloc (10);//allocated 10 bytes of region in heap area 

    p2= (char*) malloc (20);//Allocated 20 byte area in heap 

    area Free (p1);

    Free (p2);

    P1=null; Display the P1 to NULL to avoid using P1 p2=null incorrectly later

    ;
/* @filename: example-2.c * * #include <stdio.h> int main (int argc,char * argv[]) {char arr[] = "Hello World";  /* stack segment, rw---/char * p = "Hello World";

/* Text section, string direct quantity, r-x--* * arr[1] = ' l ';
* (++p) = ' l '/* error, text paragraph can not write * * * RETURN0;


PS: variable p, which is in the stack segment, but what it refers to as "Hello World" is a string of direct quantities, placed in the text segment. /* @filename: example_2_2.c * * #include <stdio.h> #include <stdlib.h> #include <string.h> char * get_str

_1 () {char str[] = "Hello World";
return str;

} char * get_str_2 () {char * str = "Hello world";
return str;

} char * Get_str_3 () {char tmp[] = "Hello World";

char * STR;

str = (char*) malloc (*sizeof (char));

memcpy (STR, TMP, 12);
return str; the int main (int argc,char * argv[]) {char*str_1 = get_str_1 ();//error, the data in the stack segment is destroyed when the function exits char*str_2 = get_str_2 ();/right, pointing to
Text paragraph in the Word constants volume, exit the program will be recycled char*str_3 = get_str_3 ();/correct, point to the data in the heap section, not free () printf ("%s\n", str_1);
printf ("%s\n", str_2);
printf ("%s\n", str_3); if (str_3!= null) {free (str_3); str_3 = null; RETUrn0; }

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.