Memory space is divided into several parts: code segment, data segment, stack, and heap (collection and sorting)

Source: Internet
Author: User

1. The function code is stored in the code segment. If the declared class has never been used, it will be optimized during compilation, and its member functions do not occupy the space of the code segment.

Global or static variables are placed in the data segment,
Place local variables in the stack,
Place the new object in the heap,

Memory is divided into 4 segments, stack zone, heap zone, code zone, and global variable zone


BSS segment: BSS segment (BSS segment) is usually a memory area used to store uninitialized global variables in the program.

BSS is short for block started by symbol. BSS segments belong to static memory allocation.

2. Code segments, data segments, and stacks are CPU-level logical concepts, while heap is language-level logical concepts.

3. There is also a constant area where the content cannot be modified.
Common char * P = "hello"; here "hello" is saved in the constant area

4. As said on the first floor, it is not appropriate to put code segments, data segments, stacks, and Stacks together.
Code segment, data segment, and stack segment are concepts
Another concept is stack, stack, global zone, and constant zone.

5. Temporary partial Stack
Heap (HEAP) Dynamics
RW (read/write) Global
RO (read-only) Code
Char * s = "Hello, world"; "h" in S is stored in the memory Ro and cannot be modified.

6. CPU register: the CPU register is actually used to control the code segment and Data Segment instructions and Data Reading. Of course, the CPU also has its own data storage location, that is, the data register in the General Register, usually the edX register. There is a register in the C language, which is to put the data in this register, so that the data can be read quite quickly, because the memory does not need to be searched, it saves the time overhead for addressing and data transmission. Some registers are used to indicate the location of the current Code segment, the location of the data segment, the location of the stack segment, and so on (note that only the corresponding code or data address is stored here, it is not the actual value. Based on this address, you can use the address bus and data bus to get the corresponding value from the memory). Otherwise, when executing the code, where can commands and data be obtained? Haha... There is also a mark register in it to identify some status bits, such as identifying Arithmetic overflow .....

----------------------------------------------------------------

Memory segments (notes)



In von noriman's architecture, the code segment, stack segment, and data segment must be included.

Because of the Feng's structure, the essence is the process of addressing and execution.



The compiler and system allocate variables from the high address.

Global variables and function parameters are stored in the memory from low to high.

Why are function parameters stored in the heap zone?

This is because our function is dynamically called during the program running.

In the compilation phase of the function, it cannot be determined how many times it will call and how much memory it will need.

It is a waste even if it is possible to allocate memory to the variable at that time.

Therefore, the compiler selects Dynamic Allocation for function parameters, that is, it dynamically allocates space for each function call.



######################################## ############



Memory is divided into 4 segments, stack zone, heap zone, code zone, and global variable zone


BSS segment: BSS segment (BSS segment) is usually a memory area used to store uninitialized global variables in the program.

BSS is short for block started by symbol. BSS segments belong to static memory allocation.


Data Segment: a data segment is usually a memory area used to store initialized global variables in the program. The data segment belongs to the static memory allocation.


Code segment: A code segment (code segment/Text Segment) is usually a memory area used to store Program Execution Code.

The size of this area is determined before the program runs, and the memory area is usually read-only. Some architectures also allow code segments to be writable, that is, programs can be modified.

In the code segment, it may also contain some read-only constant variables, such as string constants. The code segment stores the data of the program code,

If several processes on the machine run the same program, they can use the same code segment.


Heap: a heap is used to store the memory segments dynamically allocated during running of a process. Its size is not fixed,

It can be dynamically expanded or reduced. When a process calls functions such as malloc to allocate memory, the newly allocated memory is dynamically added to the stack (the heap is expanded );

When the memory is released using functions such as free, the released memory is removed from the heap (the heap is reduced)

STACK: A stack, also known as a stack, is a local variable temporarily created by a user to store programs,

That is to say, the variables defined in the ARC "{}" function (but do not include static declared variables, static means to store variables in the data segment ).

In addition, when a function is called, its parameters will also be pushed into the process stack that initiates the call. After the call is completed, the return values of the function will also be stored in the stack.

Because of the stack's first-in-first-out feature, the stack is particularly convenient for storing/restoring the call site. In this sense, we can regard the stack as a memory zone for storing and exchanging temporary data.



(1) memory segments are the same as memory pagination. segments: Permission protection, and pages: virtual memory.



(2) After segmentation, programmers can define their own segments. Each segment has an independent address space, just as the address space of a process is independent of each other.



(3) instances of the same class are allocated in one segment. Only the methods of this class can be accessed. If methods of other classes are accessed, errors may occur due to segment protection. data protection and hiding of classes can be implemented on hardware


######################################## ############################


Segmentation benefits:


Segment register ------- the upper limit of the segment address (base) and offset value (Limit) in the CPU ).

Segment address: In a valid address, if the valid address is greater than limit, an exception is thrown. In this way, the program can be restricted from data outside the current segment and cannot access data of other programs.

The benefit of object-oriented: an object is a piece of continuous data in memory.





Registers are in special form of memory and are embedded into the processor.


Each process needs to access its own region in the memory. Therefore, the memory can be divided into small segments and distributed to the process as needed.

Registers are used to store and track the segments currently maintained by processes. Offset registers is used to track the position of key data in segments.


When a process is loaded into memory, it is basically split into many small sections ). We pay more attention to the following six main sections:


(1). Text Section


The. text section is basically equivalent to the. text section of the binary executable file. It contains the machine commands used to complete program tasks.

This section is marked as read-only. If a write operation occurs, segmentation fault will occur. When a process is initially loaded into the memory, the size of this section is fixed.


(2). Data Section


The. Data section is used to store initialized variables, such as: int A = 0; the size of this section is fixed at runtime.


(3). BSS section


The lower section (. BSS) of the stack is used to store initialization variables, such as: int A; the size of this section is fixed at runtime.


(4) Heap Section


Heap section is used to store dynamically allocated variables, and the location increases from the memory address to the high address. Memory Allocation and release are controlled by the malloc () and free () functions.


(5) stack section


The stack section is used to track function calls (which may be recursive). In most systems, it increases from the memory address to the low address.

At the same time, stack growth leads to the possibility of buffer overflow.


(6) Environment/parameter Section


Environment/parameter section (Environment/arguments Section) is a copy file used to store system environment variables,

The process may need to be running. For example, a running process can use environment variables to access information such as the path, Shell Name, and host name.

This section is writable. Therefore, this section can be used in both format string and buffer overflow attacks.

In addition, the command line parameters are also kept in this region.



######################################## ########################################


Take the Win32 program as an example.

When the program is executed, the operating system maps the EXE file into the memory. The EXE file format is composed of header data and each segment of data.


The header data describes the attributes and execution environment of the EXE file. The segment data is divided into data segments, code segments, and resource segments. The length and position of the segments are described by header data.

That is to say, it is not just a code segment and a data segment. These segments are controlled by different compilation environments and compilation parameters. The Compiler automatically generates the EXE segments and file formats.

When the operating system executes the EXE, the stack segment is dynamically created and belongs to the operating system execution environment.


That is to say, the program maps to an EXE file in the memory, including data segments and code segments, which remain unchanged.

The other is the stack segment, which dynamically changes as the program runs.


1. the compiler converts source code into discrete target code (. O or. OBJ) files. The code in these files is already executable machine code or intermediate code.

However, the addresses of variables and other things are only symbols.

2. The next step is to process the target code through the linker. The main purpose is to connect the Discrete Target code into a complete executable code,

Replace the address symbol with the relative address. If an error occurs at this time, we can get an address symbol list instead of a Variable list.

3. When executing the program, the operating system allocates enough memory space. After the system support structure is established, the binary executable code is read into the memory.

During the read-in process, the first address of the memory becomes the "absolute address" of the Program (in fact, it is still a relative address, but it is a relative address in the operating system ).

So the absolute address + relative address (that is, the offset) gets the address of the variable.

Therefore, the Cs value is entered by the system, while the values of other s registers are calculated based on the additional information in the program code.

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.