Linux Process Memory layout

Source: Internet
Author: User
Tags stack pop

 

A program is essentially composed of BSS, data, and text segments three. This concept is very important in the current computer programming, and is very important in the design of the embedded system, which involves the memory size allocation of the embedded system and the storage unit occupying space.

    • BSS segment: In an architecture that uses segment memory management, the BSS segment (BSS segment) usually refers to an area of memory that is used to hold uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.
    • Data segment: In a segment-based memory management architecture, data segment usually refers to a piece of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation.
    • Code snippets: In a schema with segment memory management, the code snippet (text segment) usually refers to a piece of memory area that is used to hold the program execution code. The size of this part of the area is determined before the program runs and the memory area is read-only. In a code snippet, it is also possible to include some read-only constant variables, such as String constants.

The target file generated after the program compiles contains at least these three segments, and the approximate structure of the three segments is as follows:

Where. Text is a code snippet, read-only: The BSS segment contains uninitialized global variables and static variables in your program. The data segment contains three parts: heap, Stack (stack), and static data area.

    • Heap: A heap is used to store a dynamically allocated memory segment in a process run, which is not fixed in size and can be dynamically expanded or scaled down. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded), and freed memory is removed from the heap when memory is freed (heap is scaled down).
    • Stack: Stacks are also called stacks, which are local variables that are temporarily created by the user's program, that is, the variables defined in the parentheses "{}" (but not the variables that are static declarations, static means that the variables are stored in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.

When a program dynamically allocates space (the malloc function in C) at execution time, the allocated space belongs to the heap. Its concept differs from the concept of "heap" in data structures.

The stack segment holds variables, parameters, and return addresses inside the function, which are automatically assigned when the function is called, and access is the LIFO method in the standard stack. (because the local variables of the function are stored here, it should be accessed in a way that the stack pointer is offset, otherwise it is cumbersome to access through push, pop operations)

The static data section in the dataset holds the initialized global variables, static variables, and constants in the program.

In an architecture that uses segment memory management (such as Intel's 80x86 system), the BSS segment (Block Started by Symbol segment) usually refers to a chunk of memory that is used to hold uninitialized global variables in the program, and generally the BSS segment will be zeroed when initialized. The BSS segment is a static memory allocation, where the program is zeroed out at the outset.

For example, after a program such as C has been compiled, the initialized global variable is saved in the. Data segment, and the uninitialized global variable is saved in the. BSS segment.

Both the text and data fields are in the executable file (typically cured in an embedded system in an image file) and are loaded from the executable, while the BSS segment is not in the executable file and is initialized by the system.

Figure from "c expert programming"

The BSS segment only holds variables that have no value, so in fact it does not need to save the images of those variables. The size of the BSS segment required for the runtime is recorded in the destination file, but the BSS segment does not occupy any space in the destination file.

    1. Main.c
    2. int a = 0; //Global initialization Zone
    3. Char *p1; //Global uninitialized zone
    4. Main ()
    5. {
    6. static int c =0; //Global (Static) initialization area
    7. int b; //Stack
    8. char s[] = "abc"; //Stack
    9. Char *p2; //Stack
    10. char *p3 = "123456";  //"123456\0" in the constant area, p3 on the stack.
    11. P1 = (char *) malloc (10);
    12. P2 = (char *) malloc (20);  //The area allocated 10 and 20 bytes is in the heap area.
    13. }

(Fig.: http://www.tenouk.com/Bufferoverflowc/Bufferoverflow1c.html)

(Figure from: apue-2e, http://infohost.nmt.edu/~eweiss/222_book/222_book.html)

The computer program memory was organized into the following:

Code segment (text segment)
Data Segment
--Data (Rodata + rwdata)
--BSS
--Heap
Stack Segment

Data

The data area contains global and staticvariables used is initialized. This segment can is furtherclassified into initialized read-only (rodata) area and initialized Read-writearea (rwdata).

Bss

The BSS segment also known as uninitialized datastarts at the end of the data segment and contains all uninitialized Globa Lvariables and static variables that is initialized to zero by default.

Heap

The heap area begins at the end of the bsssegment and grows to larger addresses from there. The heap area was managed bymalloc/calloc/realloc/new and Free/delete, which may use the BRK and SBRK system calls to Adju St its size. The Heaparea is shared by all shared libraries and dynamically loaded modules in aprocess.

Stack

The stack is a LIFO structure, typically locatedin the higher parts of memory. It usually "grows" with everyregister, immediate value or stack frame being added to it. A stack frameconsists at minimum of a return address

Example Program

  1. //main.cpp
  2. int a = 0; Global initialization zone (data)
  3. Char *p1; //Global Uninitialized Zone (BSS)
  4. int main()
  5. {
  6. int b; //Stack area (stack)
  7. char s[] = "abc"; Stacking area (stack)
  8. Char *p2; //Stack area (stack)
  9. char *p3 = "123456"; P3 in the Stack area (stack); 123456\0 "in the constant area (rodata)
  10. static int c =0; //global/Static initialization zone (data)
  11. P1 = (char *)malloc ();
  12. P2 = (char *)malloc (); The allocated 10 and 20 bytes of the area are in the heap area (heap)
  13. strcpy (P1, "123456"); "123456\0" is placed in the constant area (rodata). The compiler might optimize it to a place with the "123456\0" that P3 points to.
  14. return 0;
  15. }

The difference between heaps and stacks

Management mode: For the stack, is automatically managed by the compiler, for the heap, release work by the programmer control, easy to produce memory leak.

Space size: Generally speaking, in 32-bit system, heap memory can reach nearly 4G space, from this point of view heap memory is almost no limit. But for the stack, generally there is a certain amount of space, for example, under the VC6, the default stack space size is about 1M.

Fragmentation problem: For the heap, frequent new/delete is bound to cause the memory space discontinuity, resulting in a large number of fragments, so that the program efficiency is reduced; for the stack, there is no problem, because the stack is advanced out of the queue, it is never possible to have a memory block from the middle of the stack pop.

Growth direction: For the heap, the direction of growth is upward, that is, to the memory address of the direction of increase, for the stack, its growth direction is downward, is to reduce the memory address direction of growth.

Allocation method: The heap is dynamically allocated, there is no statically allocated heap, there are 2 ways to allocate the stack: static allocation and dynamic allocation. Static allocation is done by the compiler, such as the allocation of local variables, the dynamic allocation is allocated by the Alloca function, but the dynamic allocation of the stack and the heap is different, its dynamic allocation is released by the compiler, do not need to be implemented manually.

Allocation efficiency: The stack is the machine system provides the data structure, the computer will be assigned to the bottom of the special register storage stack address, the stack stack out of a special command execution, which determines the efficiency of the stack is high; the heap is provided by the C/S library, and its mechanism is complex, for example, in order to allocate a piece of memory, The library function searches the heap memory for a sufficient amount of space available, depending on the algorithm (the specific algorithm can refer to the data structure/operating system), and if there is not enough space (possibly due to too much memory fragmentation), it is possible to call system functions to increase the memory space of the program data segment and then return. Obviously, the heap is much less efficient than the stack.

Whether it is a heap or stack, to prevent the occurrence of cross-border phenomenon.

A little discussion of Global and Static types

1. What is the difference between a static global variable and a normal global variable?

Global variables (external variables) are defined as static global variables, which are then prefixed with static.

Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored.

The difference between the two is that the scope of the non-static global variable is the entire source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program.

Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

The static global variable is only initialized once, preventing it from being referenced in other file units.

2. What is the difference between a static local variable and a normal local variable?

Changing a local variable to a static variable changes the way it is stored, which changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.

The static local variable is initialized only once, the next time based on the last result value.

3. What is the difference between a static function and a normal function?

The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions is to contain the header file. The static function has only one copy in memory (. data), and the normal function maintains a copy of each call.

Linux Process Memory layout

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.