Memory distribution C program

A typical program storage partition C contains the following categories:

  1. Text segment
  2. Initialized data segment
  3. Uninitialized data segment
  4. Stack
  5. stack

 


          A typical run-time memory layout process

 

1. Text section

Text segments typically also referred to as code segments, composed of executable instructions, the program is part of the object file or memory, Text segments are typically placed underneath the stack or heap, stack overflow to prevent tampering with their data.

Typically, Text paragraph may be shared, for programs that require frequently called, which only need to copy in memory, such as a text editor, C compiler, Shell, etc., so read-only text segment is generally in order to prevent sudden changes in the program.

 

2. initialized data segment

Initialized data segment, usually referred to simply as a data segment, the data segment occupies part of the virtual address space of the program, including internal global variables, static variables (the program is responsible for initializing these variables). It should be noted that the data segments are not read-only at run-time values ​​of variables can change.

The data section may also be divided into finer regions, and initializes the read-only and writable regions initialization.

Example: global string char s [] = "hello world", global variable int debug = 1, the static variable static int i = 10 is stored in a readable and writable area initialization; In another case, const char * string = "hello world ", the string" hello world "is stored in the read-only area initialization, string pointer is initialized writable region is present.

 

3. uninitialized data section

Uninitialized data segments, commonly referred to as "bss" section, the name comes from the old assembly operator named "block started by symbol", data in the period before the program starts executing the kernel is initialized to a zero value, typically begins initialized at the end of the data segment. The segment comprising a global variable is initialized to 0 / static variables and display source is not initialized variable.

Example: variable static int i; global variable int j; segment contained in BBS.

 

4. Stack

Stack and heap are adjacent to each other, and the opposite direction of growth; when the stack pointer touches the stack pointer position, means that stack space has been exhausted (now more and more address space, and virtual memory technology, stack and heap may be placed anywhere in the memory, but the growth is still the opposite direction).

Region contains a LIFO stack structure of the program stack, which is typically placed in a high memory address in the x86 architecture, the address 0 towards the stack growth direction, may grow in the opposite direction in other architectures. Position of the stack pointer register stack trace whenever a value is pushed onto the stack, the stack pointer is adjusted, during the invocation of a function, into a series of pressure values ​​is referred to as "stack frame" stack frame at least It includes a return address.

Stack stored information stored variables, and each automatic function is called whenever a function is called, the return address of the caller and the context, for example, some machines are stored in the stack register, a new case will be called function in the stack the re-allocation Auto / temporary variables, which is the working principle of recursive functions. Whenever the function calls itself recursively, it will use the new stack frame, so the current stack frame variables within the function entity will not affect the variables within another function entity.

 

5. heap

Stack typically used for dynamic memory allocation, starting at the end of the heap of the BSS, and the growth of the high address, typically a heap malloc, realloc, and free management, these interfaces may reuse brk / sbrk resize system call, in one process, heap space is shared by all shared libraries and dynamically loaded modules in the process.

 

Example - View storage allocation executable file

NOTE: size in bytes statistical command executable program text, data, and bss section (for more details refer to man page of size (1))

 

1. Check the following procedure C

After compiling viewing text / data / bss distribution

2. Add a global variable

After compiling an observed change, bss area increased by 4 bytes

 

3. To add a static variable

Compile and then see change, bss area increased by 4 bytes

4. Initialize the static variable to see 3

At this time, the variables stored in the data section, data of 4 bytes increases, bss reduced by 4 bytes

 5. Continue to initialize global variables to see 2

At this time, the static variable is also stored in the data section, data of 4 bytes increases, bss reduced by 4 bytes

6. Add a global const variable look

 At this time, "haha" text string stored in the segment, and an increase of five characters, it is a multi-character \ 0, ptr pointer to the data segment is stored, an increase of 4 bytes.

 

Note: In step 4/5, if it is initialized with the value 0, or the compiler will variables into BSS area. Interested students can begin to compile a look.

 

Article Translations / modified from  https://www.geeksforgeeks.org/memory-layout-of-c-program/ 

Guess you like

Origin www.cnblogs.com/miaoxiong/p/11021827.html