C ++ five memory partitions and their differences

Source: Internet
Author: User

I. Five memory partitions

"Times New Roman" "> in C ++ medium, memory splitting 5 MSO-Hansi-font-family: "Times New Roman" "> zones, they are heap, stack, free storage, and global / MSO-Hansi-font-family: "Times New Roman" "> static and constant storage.

"Times New Roman" "> stacks are allocated by the compiler as needed, the storage area of the variables that are automatically cleared when not needed. The variables are usually local variables and function parameters.

"Times New Roman" "> heap, which is composed of New "Times New Roman"> allocated memory block, their release compiler does not care, because our application Program to control, generally, a New "Times New Roman" "> A Delete "Times New Roman" ">. If the programmer does not release the program, the operating system will automatically recycle it after the program is completed.

"Times New Roman" "> free storage zone, which is composed of malloc "Times New Roman" ">, it is very similar to the heap, but it uses free "Times New Roman">.

"Times New Roman" "> global / static storage area, global variables and static variables are allocated to the same memory. In the previous C In the MSO-Hansi-font-family: "Times New Roman"> language, global variables are classified into initialized and uninitialized, in C ++ MSO-Hansi-font-family: "Times New Roman"> no such distinction exists, they share the same memory zone.

Constant storage area, which is a special storage area. It stores constants and cannot be modified (of course, you can modify them by improper means, and there are many methods)

 

Ii. Differences Between Stack and stack

The main differences between stack and stack are as follows:


1Different management methods;


2, The space size is different;


3Whether fragments can be generated is different;


4And the growth direction is different;


5Different allocation methods;


6Different allocation efficiency;

Management Method: For stacksCompilerAutomatic Management, without manual control; For the heap, the release work isProgrammer Control, Easy to generateMemory Leak.

Space size: Generally 32 In a bit system, the heap memory can reach 4G From this perspective, there is almost no limit on the heap memory. But for the stack, there is usually a certain amount of space, for example, in Vc6 Below,The default stack space is 1 m (It seems that you cannot remember ). Of course, we can modify:

  Open the project and choose the following menu: Project-> setting-> Link , In Category Selected Output And then Reserve Set the maximum value and Commit .

 Note:ReserveThe minimum value is4 byte;CommitIs retained in the page file of the virtual memory, which sets a greater value for Stack opening, which may increase the memory overhead and startup time.

 

"Times New Roman" "> fragmentation problem: for the heap, frequently New/delete "Times New Roman" "> Memory space discontinuous , resulting in a large number of fragments , reducing program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue. They are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack, before the pop-up, the post-stack content has been popped up. For details, refer to the data structure. We will not discuss it one by one here.

Growth direction: For the heap, the growth direction isUpToMemory Address increaseFor the stack, its growth direction isDownIs directed to the memoryAddress reduction.

Allocation Method:The heap is dynamically allocated, and there is no static heap allocation.. Stack has 2 Distribution Method:Static and Dynamic Allocation.Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation Alloca Function allocation ,However, the stack's dynamic allocation is different from the heap's. Its Dynamic Allocation is released by the compiler without manual implementation.

Allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer:Assign a special register to store the stack address, and execute specific commands in the output stack of the stack,This determinesStack efficiency is relatively high.The heap is C/C ++ The function library provides a complex mechanism. For example, to allocate a piece of memory, the Library FunctionAlgorithm(For specific algorithms, refer to the data structure. / Operating System) searches for available space in heap memory, If there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Apparently,Stack efficiency is much lower than stack efficiency.

From this we can see that compared with Stack New/delete UsageIt is easy to cause a large amount of memory fragments; because there is no dedicated system support, the efficiency is very low; because it may lead to switching between the user State and the core state, the memory application costs become more expensive.Therefore, stacks are the most widely used in applications, even ifFunction calls are also completed using stacks. Parameters in the function call process, return addresses, EBP And local variables are stored in stacks. . Therefore, we recommend that you use stacks instead of stacks.

"Times New Roman"> the stack has so many advantages, but it is not so flexible as compared with the stack, sometimes it is better to use heap to allocate a large amount of memory space.

Whether it is a heap or a stack, it is necessary to prevent cross-border phenomena (unless you intentionally cross-border it), because the cross-border result is either a program crash, or destroy the program's heap and stack structure to produce unexpected results.,Even if the above problem does not occur during your program running, you should be careful. Maybe it will crash at any time.DebugBut it is quite difficult :)

 

Iii. Differences between global variables and static global variables

 

The global variable itself is a static storage method, Static global variables are also static storage methods. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file.
Static global variables limit their scopes, That is, it is valid only in the source file that defines the variable,
It cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

 

From the above analysis, we can see that, Changed the local variable to a static variable.Storage MethodThat is, it changes itsLifetime. Changed the global variable to a static variable.Scope,Limits its scope of use. ThereforeStaticThis specifier plays different roles in different places. Attention should be paid.

Static

1.Static variables are allocated inStatic storage Zone, InData Segment. After the function exits, the variable value remains unchanged.

2.Scope,Global static variables and static functions can only be used in this file.(Unlike general global variables)

Local static variables are the same as local variables of the function.

 

2, Difference between struct and consortium,

 The struct allocates storage space for all elements based on the total memory space occupied by them.

 A consortium allocates storage space for the element that occupies the largest memory space.

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.