C ++ aspect

xiaoxiao2021-03-06  41

1) About Stack, System Will Allocate Memory to The Instance of Object Automatic or And to The Heap, You Must Allocate Memory To The Instance of Object with New Or Malloc Manually.

2) WHEN Function Ends, System Will Automatical Free The Memory Area of ​​Stack, But To The Heap, You Must Free The Memory Area Manually with Free Or Delete, Else It Will Result In Memory Leak.

3) The stack memory allocation operation is built into the processor's instruction set, the efficiency is high, but the assigned memory capacity is limited.

4) The memory allocated on the pile can have our own decision and use it very flexible.

Comparison of stacks and stacks

If the functions and roles of the heap and stack are common, the stack is mainly used to store the object, and the stack is mainly used to execute the program. This difference is mainly due to the characteristics of the stack and stack::

In the program, for example, C / C , all method calls are performed by the stack, all local variables, and form parameters allocate memory space from the stack. In fact, it is not allocated, just use it from the top of the stack, just like the conveyor belt in the factory, Stack Pointer automatically guides you to the place, you have to do it, just put things down. When exiting the function, modify the stack pointer to destroy the contents of the stack. This mode is the fastest, of course, it is necessary to run the program. Need to note that when allocation, such as a program that is about to call When the module assigns the data area, you should know the size of this data area in advance, that is, although the assignment is performed at the program run, how much the size is determined, the "size" is Determined time, not running at runtime.

Heap is the application operating system to assign itself to yourself during operation. Since the memory allocation from the operating system, it should take advantage of time in allocation and destruction, so the efficiency of the bunch is very low. But the advantages of the heap are The compiler does not have to know how much storage space is to be assigned from the heap, or you don't have to know how long the stored data is to stay in the stack, so much flexibility is obtained when saving data with a stack. In fact, object-oriented polymorphisms is essential, because the storage space required for polymorphic variables can only be determined after the object is created, in C , when you create an object, only You need to prepare the relevant code with the new command. When performing these code, the data is saved automatically in the stack. Of course, in order to achieve this flexibility, you will inevitably pay a certain price: I will take a longer time when allocating the storage space! This is the reason that leads to low efficiency.

Three categories in memory: Code, Data (DATA), Stack,

Where Stack is the call and return of the subroutine, the Stack is advanced, and then the next address of the current address is temporarily saved to the STACK, while the subroutine returns according to this address.

The local variables allocated in the subroutine (function) are also allocated in the Stack, so that the function is returned when the function returns.

HEAP is a data area that is particularly misappropriated and independently managed from the DATA area, which is used for dynamic allocation of data in program execution.

From the table: global static data in DATA, partially assigned static data in STACK, dynamically allocated data in HEAP.

转载请注明原文地址:https://www.9cbs.com/read-73898.html

New Post(0)