C program-Process Memory Structure Analysis

Source: Internet
Author: User

1. Each process runs in its own private memory space (virtual address space ). In a 32-bit system, the 4 GB process address dongjian is divided into user space and kernel space. User Space occupies 0 ~ 3 GB (in hexadecimal notation 0xC0000000), and the kernel space range is 3 GB ~ 4 GB. A process involves three different data segments: code segment, data segment, and stack segment. Code segment: a constant used to save the operation commands and program definitions of executable files. To prevent the code from being modified by other processes during running, the code segment can only be read and cannot be modified. Multiple processes can share the same code segment, that is, when the program is executed multiple times, the same program that runs will share the code segment. Data Segment: its location is followed by the code segment, which can be divided into initialized data segments and uninitialized data segments (also known as BSS segments ). The initialization code segment is used to store initialized global variables and static program variables, while the uninitialized data segment is used to save uninitialized global variables. Stack segment: the stack segment is used to store the dynamically allocated memory address in the process. For example, the memory space allocated by using the malloc function in C language and the new function in C ++ will be allocated in the heap. When the free function or delete function is used to release the memory, the allocated memory will be deleted from the heap. Stack is used to save the temporary variables created in the program. When a function is called, The passed parameters are stored in the stack. After a function call is completed, the returned value is stored in the stack. Stack has the features of first-in-first-out and is suitable for saving and restoring the site. The stack can be viewed in the memory area where temporary data is stored and data is exchanged. The stack size is limited by the operating system, so the space obtained from the stack is limited, while the stack size is limited by the virtual memory space, therefore, you can use the heap to obtain a large memory space. The heap location is adjacent to the data segment. 2. code replication code 1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 5 6 int etext, edata, end; // 7 8 // g_pstr and g_buffer are both global variables and are only an initialization, 9 char * g_pstr = "Global string \ n"; 10 char g_buffer [20]; 11 12 inline void disp_addr (char * p, int addr) 13 {14 printf ("Name: % s Address: % 0x \ n", p, addr); 15} 16 17 void disp_var (char * p) 18 {19 char * buffer1; 20 disp_addr ("buffer1 address:", (int) (& buffer1); 21 buffer1 = (char *) malloc (strlen (p) + 1); 22 strcpy (buffer1, p); 23 printf ("buffer1: % s \ n", buffer1); 24 free (buffer1 ); 25} 26 27 int main () 28 {29 int I = 0; 30 31 // display 32 printf ("Addr etext: % p \ n" in hexadecimal notation ", & etext); 33 printf ("Addr edata: % p \ n", & edata); 34 printf ("Addr end: % p \ n", & end ); 35 36 // display the addresses of various functions and variables 37 disp_addr ("function main ():", (int) (main); 38 disp_addr ("function disp_var ():", (int) (disp_var); 39 disp_addr ("g_pstr address:", (int) (& g_pstr); 40 disp_addr ("g_buffer address:", (int) (& g_buffer); 41 disp_addr ("I address:", (int) (& I); 42 43 disp_var (g_pstr); 44 return 0; 45} copy code 3. copy the code Addr etext: 0x8049954Addr edata: 0x8049958Addr end: 0x8049950Name: function main ():, Address: 804850 cName: function disp_var ():, Address: 80484a7Name: g_pstr address :, address: 8049930 Name: g_buffer address:, Address: 804993 cName: I address:, Address: bf8583ecName: buffer1 address:, Address: bf8583bcbuffer1: Global string

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.